Smallest number with at least n trailing zeroes in factorial
Given a number n. The task is to find the smallest number whose factorial contains at least n trailing zeroes.
Examples :
Input : n = 1
Output : 5
1!, 2!, 3!, 4! does not contain trailing zero.
5! = 120, which contains one trailing zero.
Input : n = 6
Output : 25
Approach:
In this approach, we use a while loop to iterate over each number starting from 1. For each number, we count the number of trailing zeroes in its factorial by continuously dividing it by 5 and adding the result to the answer until the number becomes less than 5. Once the count of trailing zeroes becomes greater than or equal to n, we return the current number as the answer.
Implementation of the above approach:
C++
#include <iostream>
using namespace std;
int findNum( int n) {
int num = 1;
int cnt = 0;
while ( true ) {
int temp = num;
while (temp % 5 == 0) {
cnt++;
temp /= 5;
}
if (cnt >= n) {
return num;
}
num++;
}
}
int main() {
int n = 6;
cout << findNum(n) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int findNum( int n) {
int num = 1 ;
int cnt = 0 ;
while ( true ) {
int temp = num;
while (temp % 5 == 0 ) {
cnt++;
temp /= 5 ;
}
if (cnt >= n) {
return num;
}
num++;
}
}
public static void main(String[] args) {
int n = 6 ;
System.out.println(findNum(n));
}
}
|
Python3
def findNum(n):
num = 1
cnt = 0
while True :
temp = num
while temp % 5 = = 0 :
cnt + = 1
temp / / = 5
if cnt > = n:
return num
num + = 1
n = 6
print (findNum(n))
|
C#
using System;
class Program {
static int findNum( int n) {
int num = 1;
int cnt = 0;
while ( true ) {
int temp = num;
while (temp % 5 == 0) {
cnt++;
temp /= 5;
}
if (cnt >= n) {
return num;
}
num++;
}
}
static void Main( string [] args) {
int n = 6;
Console.WriteLine(findNum(n));
}
}
|
Javascript
function findNum(n) {
let num = 1;
let cnt = 0;
while ( true )
{
let temp = num;
while (temp % 5 === 0) {
cnt += 1;
temp = Math.floor(temp / 5);
}
if (cnt >= n) {
return num;
}
num += 1;
}
}
const n = 6;
console.log(findNum(n));
|
Time Complexity: o(n log n) because we need to calculate the trailing zeroes for each number from 1 to the answer. Calculating the trailing zeroes requires dividing the current number by 5 multiple times, which takes O(log n) time in the worst case.
Auxiliary Space: O(1)
Approach:
In the article for Count trailing zeroes in factorial of a number, we have discussed number of zeroes is equal to number of 5’s in prime factors of x!. We have discussed below formula to count number of 5’s.
Trailing 0s in x! = Count of 5s in prime factors of x!
= floor(x/5) + floor(x/25) + floor(x/125) + ....
Let us take few examples to observe pattern
5! has 1 trailing zeroes
[All numbers from 6 to 9
have 1 trailing zero]
10! has 2 trailing zeroes
[All numbers from 11 to 14
have 2 trailing zeroes]
15! to 19! have 3 trailing zeroes
20! to 24! have 4 trailing zeroes
25! to 29! have 6 trailing zeroes
We can notice that, the maximum value whose factorial contain n trailing zeroes is 5*n.
So, to find minimum value whose factorial contains n trailing zeroes, use binary search on range from 0 to 5*n. And, find the smallest number whose factorial contains n trailing zeroes.
C++
#include<bits/stdc++.h>
using namespace std;
bool check( int p, int n)
{
int temp = p, count = 0, f = 5;
while (f <= temp)
{
count += temp/f;
f = f*5;
}
return (count >= n);
}
int findNum( int n)
{
if (n==1)
return 5;
int low = 0;
int high = 5*n;
while (low <high)
{
int mid = (low + high) >> 1;
if (check(mid, n))
high = mid;
else
low = mid+1;
}
return low;
}
int main()
{
int n = 6;
cout << findNum(n) << endl;
return 0;
}
|
Java
class GFG
{
static boolean check( int p, int n)
{
int temp = p, count = 0 , f = 5 ;
while (f <= temp)
{
count += temp / f;
f = f * 5 ;
}
return (count >= n);
}
static int findNum( int n)
{
if (n== 1 )
return 5 ;
int low = 0 ;
int high = 5 * n;
while (low < high)
{
int mid = (low + high) >> 1 ;
if (check(mid, n))
high = mid;
else
low = mid + 1 ;
}
return low;
}
public static void main (String[] args)
{
int n = 6 ;
System.out.println(findNum(n));
}
}
|
Python3
def check(p,n):
temp = p
count = 0
f = 5
while (f < = temp):
count + = temp / / f
f = f * 5
return (count > = n)
def findNum(n):
if (n = = 1 ):
return 5
low = 0
high = 5 * n
while (low <high):
mid = (low + high) >> 1
if (check(mid, n)):
high = mid
else :
low = mid + 1
return low
n = 6
print (findNum(n))
|
C#
using System;
class GFG
{
static bool check( int p, int n)
{
int temp = p, count = 0, f = 5;
while (f <= temp)
{
count += temp / f;
f = f * 5;
}
return (count >= n);
}
static int findNum( int n)
{
if (n == 1)
return 5;
int low = 0;
int high = 5 * n;
while (low < high)
{
int mid = (low + high) >> 1;
if (check(mid, n))
high = mid;
else
low = mid + 1;
}
return low;
}
public static void Main ()
{
int n = 6;
Console.WriteLine(findNum(n));
}
}
|
Javascript
<script>
function check(p, n)
{
let temp = p, count = 0, f = 5;
while (f <= temp)
{
count += Math.floor(temp/f);
f = f*5;
}
return (count >= n);
}
function findNum(n)
{
if (n==1)
return 5;
let low = 0;
let high = 5*n;
while (low <high)
{
let mid = (low + high) >> 1;
if (check(mid, n))
high = mid;
else
low = mid+1;
}
return low;
}
let n = 6;
document.write(findNum(n) + "<br>" );
</script>
|
PHP
<?php
function check( $p , $n )
{
$temp = $p ; $count = 0; $f = 5;
while ( $f <= $temp )
{
$count += $temp / $f ;
$f = $f * 5;
}
return ( $count >= $n );
}
function findNum( $n )
{
if ( $n == 1)
return 5;
$low = 0;
$high = 5 * $n ;
while ( $low < $high )
{
$mid = ( $low + $high ) >> 1;
if (check( $mid , $n ))
$high = $mid ;
else
$low = $mid + 1;
}
return $low ;
}
$n = 6;
echo (findNum( $n ));
?>
|
Time Complexity: O(log2N)
We take log2N in binary search and our check() function takes log5N time so the overall time complexity becomes log2N * log5N which in a more general sense can be written as (logN)2 which can also be written as log2N.
Auxiliary Space: O(1)
As constant extra space is used.
Last Updated :
07 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...