How to find the smallest number with just 0 and 7

2020-07-26 03:21发布

This is one of algorithmic problem I encountered in one of interview. Unable to figure out how to solve it in most efficient way.

2条回答
再贱就再见
2楼-- · 2020-07-26 03:45

According by THIS QUESTION, in http://oeis.org/ you can find this class of number sequences: Check your's here.

a(n) = min{A204094(k): k > 0 and A204094(k) mod n = 0}

Simply adapt the algorithm to fit your needs.

查看更多
smile是对你的礼貌
3楼-- · 2020-07-26 03:58

Here is my suggested code. It finds the smallest number with 0 and 7 (except the number 0) within the long range. In this case, I'm looking for the result for 11.

public class Class007
{
   static long NUM = 11;
   public static void main(String[] args)
   {
       //NUM is the given number
       //find007() finds the smallest number with 0 & 7 that is divided by NUM
       System.out.print(find007(NUM));
   }

     static long find007(long n){

       if(is007(n))
         return n;

       if(n+NUM<n)
         return 0;

       return find007(n+NUM);
     }
     static boolean is007(long n){
        while(n!=0 && (n%10==0 || n%10==7))
         n=n/10;

       return n==0;
     }
}
查看更多
登录 后发表回答