How to find the smallest number from 0 and 7, which is divided by the given number?

This is one of the algorithmic problems that I encountered in an interview. It is impossible to figure out how to solve it in the most efficient way.

+2
source share
2 answers

Here is my suggested code. It finds the smallest number with 0 and 7 (except for the number 0) in the long range. In this case, I am looking for a 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; } } 
+1
source

According to THIS QUESTION , at http://oeis.org/ you can find this class of number sequences: Check here .

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

Just adapt the algorithm to your needs.

0
source

Source: https://habr.com/ru/post/988998/


All Articles