Smallest N digit number divisible by all possible prime digits
Given an integer N, the task is to find the smallest N digit number divisible by all possible prime digits, i.e, 2, 3, 5 and 7. Print -1 if no such number is possible.
Input: N = 5
Explanation: 10080 is the smallest five-digit number that is divisible by 2, 3, 5 and 7.
Input: N = 3
Follow the steps given below to solve the problem:
- Since all the four numbers 2, 3, 5, 7 are prime it means N will also be divisible by their product 2 × 3 × 5 × 7 = 210
- For N < 3, no such number exists. So, print -1.
- For N = 3, the answer will be 210.
- For N > 3, the following computation needs to be done:
- Find Remainder R = 10N-1 % N.
- Add 210 – R to 10N-1.
Below is the implementation of the above approach:
Time complexity: O(logN)
Auxiliary Space: O(1)
My Personal Notes arrow_drop_up
Please Login to comment...