# How to find numbers whose factorials end with n zeros in Python

In this tutorial, we’re going to learn how to find numbers whose factorials end with ‘n’ zeros in Python. But first, we need to understand how we implement our logic and find the list of numbers whose factorials end with ‘n’ zeros. We need to first create a function that finds the number of trailing zeros present in the factorial of a specified number. Next, we need to efficiently search for numbers from the entire numeric range which satisfy the criteria and append them to a list. In the end, we just need to print the list.

## Python program to find numbers whose factorials end with ‘n’ zeros

list=[]; def count(n): c=0 while (n>0): n =int(n/5); c=c+n; return c; def search( n ): f=0; l=1e6; while (f<l): mid=int((f+l)/2); no=count(mid); if (no < n): f=mid+1; else: l=mid; while (count(f)==n): list.append(f); f=f+1; for i in range(len(list)): print(list[i]," ") num=input("Enter the number of trailing zeros:"); n=int(num); search(n);

In this program, the function ‘`count()`

‘ returns the number of trailing zeros present in the factorial of a specific number. Function ‘`search()`

‘ is where we implement the binary search algorithm to efficiently find out numbers satisfying the criteria. When a number satisfies the condition, we append the same in the list. At the end of the function, we print the list displaying all the numbers whose factorials have ‘n’ zeros.

Output generated will be:

Enter the number of trailing zeros: 2 10 11 12 13 14

You can also check out this, for a better understanding of the implementation:

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

## Leave a Reply