Python program to find the number of trailing zero in factorial of a large number

In this tutorial, We will seeĀ how to find the number of trailing zero in factorial of a large number in Python. A solution that comes to our mind is to just find factorial of a large number and count the trailing zero that’s ok but this will take more time to solve this problem that is time limit exceeded. So, we will learn to solve this problem in a simple way which takes less time for compilation. Before going to solve we will learn what is factorial and how to find factorial of a large number in Python.

What is the factorial of a number?

Factorial of a number(N) greater than or equal to 1 is the product of all integer less than or equal to N but greater than or equal to 1. Factorial of zero is equal to 1.

Python program to find factorial of a large number

Python program:-

import math
f=math.factorial 
n=100
s=f(n)
print(s)

Here, We have included math module by using the import function to find factorial.

Output:-

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Python program to find the number of trailing zero in factorial of a large number

Now, We have learned to find factorial of a large number. So, we are going to find trailing zero of factorial of a large number simply. First of all, we will see the algorithm which we will use to solve this problem.

  • Keep dividing the given value N by Power of 5(i) and update the value of count is the sum of n/I while n/i is greater than or equal to 1.

Python program:-

n=100
count=0
i=5
while(n/i>=1):
count+=int(n/i)
i=i*5
print('Number of trailing zero:')
print(count)

Output:-

Number of trailing zero:
24

So Guy’s, I hope you find it useful.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *