VOOZH about

URL: https://www.geeksforgeeks.org/dsa/smallest-number-divisible-n-least-k-trailing-zeros/

⇱ Smallest number divisible by n and has at-least k trailing zeros - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Smallest number divisible by n and has at-least k trailing zeros

Last Updated : 16 Feb, 2023

Two integers n and k are given. Our task is to print K-rounding of n. K-rounding is the minimum positive integer X, such that x ends with k or more zeros and is divisible by n.
Examples : 
 

Input : n = 30, k = 3.
Output : 3000
3000 is the smallest number that
has at-least k 0s and is divisible
by n.

Input : n = 375, k = 4.
Output : 30000


 


Method 1 : 
The brute force approach is to start with result = 10k. Check if result is divided by n. If yes, it's the answer, else increase it by 10k
Method 2 : The efficient approach is to calculate the LCM of 10k and n. 
Suppose, n = 375, k = 4. 
result = 10000. 
Now, LCM of 375 and 10000 is the lowest number divided by both of them. 
It will contain k or more zeros (because it is multiple of 10k) and will be a multiple of n as well.
Below is the implementation : 
 

Output : 
 

30000

Time Complexity: O(logk + log(max(10k, n)), where n and k are the given integers.

Auxiliary Space: O(1), no extra space is required, so it is a constant.


 

Comment