Smallest number divisible by n and has at-least k trailing zeros
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 :
C++
#include <bits/stdc++.h>
using namespace std;
long long getRounding( long long n, long long k)
{
long long rounding = pow (10, k);
long long result = __gcd(rounding, n);
return ((rounding * n) / result);
}
int main()
{
long long n = 375, k = 4;
cout << getRounding(n, k);
return 0;
}
|
Java
import java.util.*;
class GFG {
static long gcd( long a, long b)
{
if (a == 0 || b == 0 )
return 0 ;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
public static long getRounding( long n, long k)
{
long rounding = ( long )Math.pow( 10 , k);
long result = gcd(rounding, n);
return ((rounding * n) / result);
}
public static void main(String[] args)
{
long n = 375 , k = 4 ;
System.out.println( getRounding(n, k));
}
}
|
Python3
def gcd(a, b):
if (a = = 0 or b = = 0 ):
return 0
if (a = = b):
return a
if (a > b):
return gcd(a - b, b)
return gcd(a, b - a)
def getRounding(n, k):
rounding = pow ( 10 , k);
result = gcd(rounding, n)
return ((rounding * n) / result)
n = 375
k = 4
print ( int (getRounding(n, k)))
|
C#
using System;
class GFG {
static long gcd( long a, long b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
public static long getRounding( long n, long k)
{
long rounding = ( long )Math.Pow(10, k);
long result = gcd(rounding, n);
return ((rounding * n) / result);
}
public static void Main()
{
long n = 375, k = 4;
Console.Write( getRounding(n, k));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0 || $b == 0)
return 0;
if ( $a == $b )
return $a ;
if ( $a > $b )
return gcd( $a - $b , $b );
return gcd( $a , $b - $a );
}
function getRounding( $n , $k )
{
$rounding = intval (pow(10, $k ));
$result = gcd( $rounding , $n );
return intval (( $rounding * $n ) /
$result );
}
$n = 375;
$k = 4;
echo getRounding( $n , $k );
?>
|
Javascript
<script>
function gcd(a , b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
function getRounding(n , k)
{
var rounding = Math.pow(10, k);
var result = gcd(rounding, n);
return ((rounding * n) / result);
}
var n = 375, k = 4;
document.write(getRounding(n, k));
</script>
|
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.
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...