How to find the smallest number with just 0 and 7 which is divided by a given number? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2020-01-23 02:42:11

问题


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


回答1:


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;
     }
}



回答2:


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.



来源:https://stackoverflow.com/questions/29298656/how-to-find-the-smallest-number-with-just-0-and-7-which-is-divided-by-a-given-nu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!