Numbers whose Factorials end with n Zeros


A factorial of a number is the product of all positive integers up to the given number. For example, the factorial of 5 is denoted as 5! and is equal to the product of all positive integers up to 5:

5! = 5 x 4 x 3 x 2 x 1 = 120

The number of zeros at the end of the decimal representation of a number's factorial is referred to as the "trailing zeros" in a factorial. The factorial of 5, for instance, is 120, which has one trailing zero, while the factorial of 10, on the other hand, is 3,628,800, which has two trailing zeros.

Problem Statement

Given an integer n, we must determine the number of positive integers whose factorial has n trailing zeros.

Sample Examples

Input

n = 1

Output

5 6 7 8 9

Explanation

5! = 120

6!= 720

7! = 5040

8! = 40320

9! = 362880

It can be observed that the factorials of all the output numbers have n trailing zeros i.e. one trailing zero.

Input

n = 2

Output

10 11 12 13 14

Explanation

10! = 3628800

11! = 39916800

12! = 479001600

13! = 6227020800

14! = 87178291200

It can be observed that the factorials of all the output numbers have n trailing zeros i.e. two trailing zeros.

Input

n = 5

Output

No Output

Explanation

25! = 15511210043330985984000000 has 6 trailing zeros.

A number with exactly 5 trailing zeros in its factorial would have 5 factors of 5 in its prime factorization, but would not have any additional factors of 5. However the smallest number with 5 factors of 5 in its prime factorization is 25!, which has 6 zeros in its factorial.

Naive Solution Approach

We simply iterate over a range of integers(1 to 106). For each number, we check if the number of zeros in its factorial is equal to the given number n. If it is, we add it to the ans vector. If the number of trailing zeros in the factorial exceeds the given number, n, we break the loop.

This approach does not work for larger numbers as overflow occurs.

Algorithm

Function factorial(n):

Initialize fact = 1
for i = 2 to n:
   fact = fact * i
return fact

Function count_trailing_zeros(num):

Initialize count = 0
while num % 10 = 0:
    count = count + 1
    num = num / 10
return count

Function find_numbers_with_n_trailing_zeros(n):

Initialize ans = empty vector
for i = 1 to 1e6:
    a = count_trailing_zeros(factorial(i))
    if a = n:
        ans.push_back(i)
    else if a > n:
        break
if size of ans = 0:
    print "No Output"
else:
    for x in ans:
       print x

Time and Space Complexity Analysis

Time Complexity: O(n*log n)

The time complexity of this code is O(n*log n) because the factorial() function has a time complexity of O(n) and it is being called for n values in the for loop, resulting in a total time complexity of O(n^2). However, the loop is broken early if the number of trailing zeros exceeds the input value n, which reduces the number of iterations significantly.

Space Complexity: O(n)

The space complexity is O(n) because the program uses a vector to store the result.

Optimized Approach

The technique seeks all numbers with a specified number of trailing zeros in their factorial. We use a binary search strategy to identify the first number with the specified number of trailing zeros, and then iterate over all subsequent numbers with the same number of trailing zeros until we find a number who does not have 'n' trailing zeros.

The approach consists of the following steps:

  • Define a function count_trailing_zeros() that takes an integer num and returns the count of trailing zeros in the factorial of num.

  • Define a function find_numbers_with_n_trailing_zeros() that takes an integer n as input and returns a vector of integers that have n trailing zeros in their factorial.

  • Use binary search to find the first number with n trailing zeros.

  • Push all the numbers after the start with n trailing zeros to ans.

  • Return ans.

Algorithm

Function to count trailing zeros of the given factorial number(num):

count = 0
while num > 0:
    num = num / 5
    count += num
return count

Function to find the number which have n trailing zeros(n):

start = 0
end = maximum integer value
while start < end:
    mid = (start + end) / 2
    count = count_trailing_zeros(mid)
    if count < n:
        start = mid + 1
    else:
        end = mid
ans = empty vector
while count_trailing_zeros(start) == n:
    ans.push_back(start)
    start++
return ans

Function to print the vector(ans):

for i = 0 to ans.size():
print ans[i] + " "

Main function:

n = 3
result = find_numbers_with_n_trailing_zeros(n)
print(result)

Example: C++ Program

In the following program, to return the numbers whose factorials have ‘n’ trailing zeros we use binary search and the concept of prime factors. The idea is to consider prime factors of a factorial n. A trailing zero is always produced by prime factors 2 and 5. It can be easily observed 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 can find out the number of trailing zeros. We then use binary search to determine the numbers which have ‘n’ trailing zeros in its factorial.

// C++ program to find numbers which have ‘n’ trailing zeros in their factorial
#include <bits/stdc++.h>
using namespace std;
// function to count trailing zeros of the given factorial number
int count_trailing_zeros(int num){
    int count = 0;
    while (num > 0){
        num /= 5;
        count += num;
    }
    return count;
}
// function to find the number which have n trailing zeros
vector<int> find_numbers_with_n_trailing_zeros(int n){
    int start = 0;
    int end = INT_MAX;
    // binary search for first number with n trailing zeros
    while (start < end){
        int mid = (start + end) / 2;
        int count = count_trailing_zeros(mid);
        if (count < n)
            start = mid + 1;
        else
            end = mid;
    }
    // push all numbers after low with n trailing zeros.
    vector<int> ans;
    while (count_trailing_zeros(start) == n){
        ans.push_back(start);
        start++;
    }   
    return ans;
}
void print(vector<int> &ans){
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << " ";
}
// driver function
int main(){
    int n = 3;
    vector<int> result = find_numbers_with_n_trailing_zeros(n);
    print(result);
    return 0;
}

Output

15 16 17 18 19

Time and Space Complexity Analysis

Time Complexity: O(n)

The time complexity of the code is O(log n) because it uses binary search to find the first number with n trailing zeros, which reduces the search space by half at each iteration.

Space Complexity: O(m)

The space complexity is O(m), where m is the number of numbers with n trailing zeros, since the program stores all these numbers in a vector.

Conclusion

This article discusses two approaches to find Numbers whose Factorials end with n Zeros. The concept of the approaches, examples, algorithm used, C++ program solution as well as the time and space complexity analysis were explained thoroughly for a deeper understanding.

Updated on: 08-Sep-2023

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements