Find the smallest number X such that X! contains at least Y trailing zeros.
Given an integer Y, find the smallest number X such that X! contains at least Y trailing zeros.
Prerequisites – Count trailing zeroes in factorial of a number
Examples:
Input : Y = 2
Output : 10
10! = 3628800, which has 2 trailing zeros. 9! = 362880, which has 1 trailing zero. Hence, 10 is the correct answer.
Input : Y = 6
Output : 25
25! = 15511210043330985984000000, which has 6 trailing zeros. 24! = 620448401733239439360000, which has 4 trailing zeros. Hence, 25 is the correct answer.
Approach: The problem can be easily solved by using Binary Search. The number of trailing zeros in N! is given by the count of the factors 5 in N!.Read this article for prerequisites. The countFactor(5, N) function returns the count of factor 5 in N! which is equal to count of trailing zeros in N!. The smallest number X such that X! contains at least Y trailing zeros can be computed quickly by using binary search on a range [0, 5 * Y] using this function.
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countFactor( int P, int X)
{
if (X < P)
return 0;
return (X / P + countFactor(P, X / P));
}
int findSmallestX( int Y)
{
int low = 0, high = 5 * Y;
int N = 0;
while (low <= high) {
int mid = (high + low) / 2;
if (countFactor(5, mid) < Y) {
low = mid + 1;
}
else {
N = mid;
high = mid - 1;
}
}
return N;
}
int main()
{
int Y = 10;
cout << findSmallestX(Y);
return 0;
}
|
Java
class GFG
{
static int countFactor( int P, int X)
{
if (X < P)
return 0 ;
return (X / P + countFactor(P, X / P));
}
static int findSmallestX( int Y)
{
int low = 0 , high = 5 * Y;
int N = 0 ;
while (low <= high)
{
int mid = (high + low) / 2 ;
if (countFactor( 5 , mid) < Y)
{
low = mid + 1 ;
}
else
{
N = mid;
high = mid - 1 ;
}
}
return N;
}
public static void main(String args[])
{
int Y = 10 ;
System.out.println(findSmallestX(Y));
}
}
|
Python3
def countFactor(P, X):
if (X < P):
return 0 ;
return (X / / P + countFactor(P, X / / P));
def findSmallestX(Y):
low = 0 ;
high = 5 * Y;
N = 0 ;
while (low < = high):
mid = (high + low) / / 2 ;
if (countFactor( 5 , mid) < Y):
low = mid + 1 ;
else :
N = mid;
high = mid - 1 ;
return N;
Y = 10 ;
print (findSmallestX(Y));
|
C#
class GFG
{
static int countFactor( int P, int X)
{
if (X < P)
return 0;
return (X / P + countFactor(P, X / P));
}
static int findSmallestX( int Y)
{
int low = 0, high = 5 * Y;
int N = 0;
while (low <= high)
{
int mid = (high + low) / 2;
if (countFactor(5, mid) < Y)
{
low = mid + 1;
}
else
{
N = mid;
high = mid - 1;
}
}
return N;
}
static void Main()
{
int Y = 10;
System.Console.WriteLine(findSmallestX(Y));
}
}
|
PHP
<?php
function countFactor( $P , $X )
{
if ( $X < $P )
return 0;
return ((int)( $X / $P ) +
countFactor( $P , ( $X / $P )));
}
function findSmallestX( $Y )
{
$low = 0; $high = 5 * $Y ;
$N = 0;
while ( $low <= $high )
{
$mid = (int)(( $high + $low ) / 2);
if (countFactor(5, $mid ) < $Y )
{
$low = $mid + 1;
}
else
{
$N = $mid ;
$high = $mid - 1;
}
}
return $N ;
}
$Y = 10;
echo (findSmallestX( $Y ));
?>
|
Javascript
<script>
function countFactor(P, X)
{
if (X < P)
return 0;
return (parseInt(X / P) +
countFactor(P, parseInt(X / P)));
}
function findSmallestX(Y)
{
let low = 0, high = 5 * Y;
let N = 0;
while (low <= high)
{
let mid = parseInt((high + low) / 2);
if (countFactor(5, mid) < Y)
{
low = mid + 1;
}
else
{
N = mid;
high = mid - 1;
}
}
return N;
}
let Y = 10;
document.write(findSmallestX(Y));
</script>
|
Time Complexity: O(log2Y * log5Y)
Space Complexity: O(log5Y)
The extra space is used due to recursion call stack of countFactor() function.
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...