Count trailing zeroes in factorial of a number
Given an integer n, write a function that returns count of trailing zeroes in n!.
Examples :
Input: n = 5
Output: 1
Explanation: Factorial of 5 is 120 which has one trailing 0.
Input: n = 20
Output: 4
Explanation: Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes.
Input: n = 100
Output: 24
Count trailing zeroes by counting occurrence of 5 in the factorial:
A simple method is to first calculate factorial of n, then count trailing 0s in the result but this can cause overflow for bigger numbers. The idea is to consider prime factors of a factorial n. A trailing zero is always produced by prime factors 2 and 5. If we can count the number of 5s and 2s, our task is done.
Illustration:
Consider the following examples:
Input: n = 5: There is one 5 and three 2s in prime factors of 5! (2 * 2 * 2 * 3 * 5). So a count of trailing 0s is 1.
Input: n = 11: There are two 5s and eight 2s in prime factors of 11! (2 8 * 34 * 52 * 7). So the count of trailing 0s is 2.
We can observe that the number of 2s in prime factors is always more than or equal to the number of 5s. So, if we count 5s in prime factors, we are done.
How to count the total number of 5s in prime factors of n! ?
A simple way is to calculate floor(n/5). For example, 7! has one 5, 10! has two 5s. But, numbers like 25, 125, etc have more than 5 instead of floor (n / 5). For example, if we consider 28! we get one extra 5 and the number of 0s becomes 6. Handling this is simple, first, divide n by 5 and remove all single 5s, then divide by 25 to remove extra 5s, and so on.
Following is the summarized formula for counting trailing 0s.
Trailing 0s in n! = Count of 5s in prime factors of n! = floor(n/5) + floor(n/25) + floor(n/125) + ….
Steps to solve the above problem:
- Initialize a variable count = 0 to keep track of trailing zeros.
- Iterate a loop over all the powers of 5 until the power exceeds n:
- Divide n by the current power of 5 and add the quotient to count.
- Return count as the final answer.
Below is the implementation of the approach:
C++
#include <iostream>
using namespace std;
int findTrailingZeros( int n)
{
if (n < 0)
return -1;
int count = 0;
for ( int i = 5; n / i >= 1; i *= 5)
count += n / i;
return count;
}
int main()
{
int n = 100;
cout << "Count of trailing 0s in " << 100 << "! is "
<< findTrailingZeros(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findTrailingZeros( int n)
{
if (n < 0 )
return - 1 ;
int count = 0 ;
for ( int i = 5 ; n / i >= 1 ; i *= 5 )
count += n / i;
return count;
}
public static void main(String[] args)
{
int n = 100 ;
System.out.println( "Count of trailing 0s in " + n
+ "! is "
+ findTrailingZeros(n));
}
}
|
Python3
def findTrailingZeros(n):
if (n < 0 ):
return - 1
count = 0
while (n > = 5 ):
n / / = 5
count + = n
return count
n = 100
print ( "Count of trailing 0s " +
"in 100! is" , findTrailingZeros(n))
|
C#
using System;
class GFG
{
static int findTrailingZeros( int n)
{
if (n < 0)
return -1;
int count = 0;
for ( int i = 5; n / i >= 1; i *= 5)
count += n / i;
return count;
}
public static void Main ()
{
int n = 100;
Console.WriteLine( "Count of trailing 0s in " +
n + "! is " +
findTrailingZeros(n));
}
}
|
Javascript
<script>
function findTrailingZeros(n)
{
if (n < 0)
return -1;
let count = 0;
for (let i = 5; Math.floor(n / i) >= 1; i *= 5)
count += Math.floor(n / i);
return count;
}
let n = 100;
document.write( "Count of trailing 0s in " + 100
+ "! is " + findTrailingZeros(n));
</script>
|
PHP
<?php
function findTrailingZeros( $n )
{
if ( $n < 0)
return -1;
$count = 0;
for ( $i = 5; $n / $i >= 1; $i *= 5)
$count += $n / $i ;
return $count ;
}
$n = 100;
echo "Count of trailing 0s in " , 100,
"! is " , findTrailingZeros( $n );
?>
|
Output
Count of trailing 0s in 100! is 24
Time Complexity: O(log5n)
Auxiliary Space: O(1)
Last Updated :
06 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...