Given x, y, z and n, find smallest n digit number which is divisible by x, y and z.
Input : x = 2, y = 3, z = 5 n = 4 Output : 1020 Input : x = 3, y = 5, z = 7 n = 2 Output : Not possible
1) Find smallest n digit number is pow(10, n-1).
2) Find LCM of given 3 numbers x, y and z.
3) Find remainder of the LCM when divided by pow(10, n-1).
4) Add the “LCM – remainder” to pow(10, n-1). If this addition is still a n digit number, we return the result. Else we return Not possible.
Suppose n = 4 and x, y, z are 2, 3, 5 respectively.
1) First find the least four digit number i.e. 1000,
2) LCM of 2, 3, 5 so the LCM is 30.
3) Find the reminder of 1000 % 30 = 10
4) Subtract the remainder from LCM, 30 – 10 = 20. Result is 1000 + 20 = 1020.
Below is the implementation of above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Smallest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X
- Java Program for Smallest K digit number divisible by X
- Sum of n digit numbers divisible by a given number
- Count n digit numbers divisible by given number
- Largest N digit number divisible by given three numbers
- Smallest number divisible by first n numbers
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Smallest and Largest sum of two n-digit numbers
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- N digit numbers divisible by 5 formed from the M digits
- Count of N digit palindromic numbers divisible by 9
- Largest and Smallest N-digit Octal Numbers
- Largest and Smallest N-digit Hexadecimal Numbers
- Count of numbers in range which are divisible by M and have digit D at odd places
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar