Open In App

Numbers whose factorials end with n zeros

Last Updated : 14 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer n, we need to find the number of positive integers whose factorial ends with n zeros.
Examples: 
 

Input : n = 1
Output : 5 6 7 8 9
Explanation: Here, 5! = 120, 6! = 720,
7! = 5040, 8! = 40320 and 9! = 362880.

Input : n = 2
Output : 10 11 12 13 14 
          

 

Prerequisite : Trailing zeros in factorial.
Naive approach:We can just iterate through the range of integers and find the number of trailing zeros of all the numbers and print the numbers with n trailing zeros.
Efficient Approach:In this approach we use binary search. Use binary search for all the numbers in the range and get the first number with n trailing zeros. Find all the numbers with m trailing zeros after that number.
 

C++




// Binary search based CPP program to find
// numbers with n trailing zeros.
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate trailing zeros
int trailingZeroes(int n)
{
    int cnt = 0;
    while (n > 0) {
        n /= 5;
        cnt += n;
    }
    return cnt;
}
 
void binarySearch(int n)
{
    int low = 0;
    int high = 1e6; // range of numbers
 
    // binary search for first number with
    // n trailing zeros
    while (low < high) {
        int mid = (low + high) / 2;
        int count = trailingZeroes(mid);
        if (count < n)
            low = mid + 1;
        else
            high = mid;
    }
 
    // Print all numbers after low with n
    // trailing zeros.
    vector<int> result;
    while (trailingZeroes(low) == n) {
        result.push_back(low);
        low++;
    }
 
    // Print result
    for (int i = 0; i < result.size(); i++)
        cout << result[i] << " ";
}
 
// Driver code
int main()
{
    int n = 2;
    binarySearch(n);
    return 0;
}


Java




// Binary search based Java
// program to find numbers
// with n trailing zeros.
import java.io.*;
 
class GFG {
 
    // Function to calculate
    // trailing zeros
    static int trailingZeroes(int n)
    {
        int cnt = 0;
        while (n > 0)
        {
            n /= 5;
            cnt += n;
        }
        return cnt;
    }
 
    static void binarySearch(int n)
    {
        int low = 0;
         
        // range of numbers
        int high = 1000000;
 
        // binary search for first number
        // with n trailing zeros
        while (low < high) {
            int mid = (low + high) / 2;
            int count = trailingZeroes(mid);
            if (count < n)
                low = mid + 1;
            else
                high = mid;
        }
 
        // Print all numbers after low
        // with n trailing zeros.
        int result[] = new int[1000];
        int k = 0;
        while (trailingZeroes(low) == n) {
            result[k] = low;
            k++;
            low++;
        }
 
        // Print result
        for (int i = 0; i < k; i++)
            System.out.print(result[i] + " ");
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 3;
        binarySearch(n);
    }
}
 
// This code is contributed
// by Nikita Tiwari.


Python3




# Binary search based Python3 code to find
# numbers with n trailing zeros.
 
# Function to calculate trailing zeros
def trailingZeroes( n ):
    cnt = 0
    while n > 0:
        n =int(n/5)
        cnt += n
    return cnt
 
def binarySearch( n ):
    low = 0
    high = 1e6  # range of numbers
     
    # binary search for first number with
    # n trailing zeros
    while low < high:
        mid = int((low + high) / 2)
        count = trailingZeroes(mid)
        if count < n:
            low = mid + 1
        else:
            high = mid
             
    # Print all numbers after low with n
    # trailing zeros.
    result = list()
    while trailingZeroes(low) == n:
        result.append(low)
        low+=1
     
    # Print result
    for i in range(len(result)):
        print(result[i],end=" ")
 
# Driver code
n = 2
binarySearch(n)
 
# This code is contributed by "Sharad_Bhardwaj".


C#




// Binary search based C#
// program to find numbers
// with n trailing zeros.
using System;
 
class GFG {
 
    // Function to calculate
    // trailing zeros
    static int trailingZeroes(int n)
    {
        int cnt = 0;
         
        while (n > 0)
        {
            n /= 5;
            cnt += n;
        }
         
        return cnt;
    }
 
    static void binarySearch(int n)
    {
        int low = 0;
         
        // range of numbers
        int high = 1000000;
 
        // binary search for first number
        // with n trailing zeros
        while (low < high) {
            int mid = (low + high) / 2;
            int count = trailingZeroes(mid);
             
            if (count < n)
                low = mid + 1;
            else
                high = mid;
        }
 
        // Print all numbers after low
        // with n trailing zeros.
        int []result = new int[1000];
        int k = 0;
        while (trailingZeroes(low) == n) {
            result[k] = low;
            k++;
            low++;
        }
 
        // Print result
        for (int i = 0; i < k; i++)
            Console.Write(result[i] + " ");
    }
 
    // Driver code
    public static void Main()
    {
        int n = 2;
         
        binarySearch(n);
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// Binary search based PHP program to
// find numbers with n trailing zeros.
 
// Function to calculate trailing zeros
function trailingZeroes($n)
{
    $cnt = 0;
    while ($n > 0)
    {
        $n = intval($n / 5);
        $cnt += $n;
    }
    return $cnt;
}
 
function binarySearch($n)
{
    $low = 0;
    $high = 1e6; // range of numbers
 
    // binary search for first number
    // with n trailing zeros
    while ($low < $high)
    {
        $mid = intval(($low + $high) / 2);
        $count = trailingZeroes($mid);
        if ($count < $n)
            $low = $mid + 1;
        else
            $high = $mid;
    }
 
    // Print all numbers after low with n
    // trailing zeros.
    $result = array();
    while (trailingZeroes($low) == $n)
    {
        array_push($result, $low);
        $low++;
    }
 
    // Print result
    for ($i = 0;
         $i < sizeof($result); $i++)
        echo $result[$i] . " ";
}
 
// Driver code
$n = 2;
binarySearch($n);
 
// This code is contributed by Ita_c
?>


Javascript




<script>
 
// Binary search based JavaScript program to find
// numbers with n trailing zeros.
 
// Function to calculate trailing zeros
function trailingZeroes(n)
{
    var cnt = 0;
    while (n > 0) {
        n = parseInt(n/5);
        cnt += n;
    }
    return cnt;
}
 
function binarySearch(n)
{
    var low = 0;
    var high = 1e6; // range of numbers
 
    // binary search for first number with
    // n trailing zeros
    while (low < high) {
        var mid = parseInt((low + high) / 2);
        var count = trailingZeroes(mid);
        if (count < n)
            low = mid + 1;
        else
            high = mid;
    }
 
    // Print all numbers after low with n
    // trailing zeros.
    var result = [];
    while (trailingZeroes(low) == n) {
        result.push(low);
        low++;
    }
 
    // Print result
    for (var i = 0; i < result.length; i++)
        document.write( result[i] + " ");
}
 
// Driver code
var n = 2;
binarySearch(n);
 
 
</script>


Output: 

10 11 12 13 14 

Time Complexity: O(log(m)). Here m=1e6

Auxiliary Space: O(k) where k is the number of trailing zeros.
 



Similar Reads

Count natural numbers whose factorials are divisible by x but not y
Given two numbers x and y (x &lt;= y), find out the total number of natural numbers, say i, for which i! is divisible by x but not y. Examples : Input : x = 2, y = 5 Output : 3 There are three numbers, 2, 3 and 4 whose factorials are divisible by x but not y. Input: x = 15, y = 25 Output: 5 5! = 120 % 15 = 0 &amp;&amp; 120 % 25 != 0 6! = 720 % 15 =
7 min read
Minimum number of Factorials whose sum is equal to N
Given a number N (&lt;1010), the task is to find the minimum number of factorials needed to represent N, as their sum. Also, print those factorials.Examples: Input: N = 30 Output: 2 24, 6 Explanation: Factorials needed to represent 30: 24, 6 Input: N = 150 Output: 3 120, 24, 6 Explanation: Factorials needed to represent 150: 120 24 6 Approach: In o
7 min read
GCD of factorials of two numbers
Given two numbers m and n. Find the GCD of their factorial.Examples : Input : n = 3, m = 4Output : 6Explanation:Factorial of n = 1 * 2 * 3 = 6Factorial of m = 1 * 2 * 3 * 4 = 24GCD(6, 24) = 6. Input : n = 9, m = 5Output : 20Explanation:Factorial of n = 1 * 2 * 3 *4 * 5 * 6 * 7 * 8 * 9 = 362880Factorial of m = 1 * 2 * 3 * 4 * 5 = 120GCD(362880, 120)
4 min read
Sum of factorials of Prime numbers in a Linked list
Given a Linked list of N integers, the task is to find the sum of factorials of each prime element in the list.Examples: Input: L1 = 4 -&gt; 6 -&gt; 2 -&gt; 12 -&gt; 3 Output: 8 Explanation: Prime numbers are 2 and 3, hence 2! + 3! = 2 + 6 = 8.Input: L1 = 7 -&gt; 4 -&gt; 5 Output: 5160 Explanation: Prime numbers are 7 and 5, hence 7! + 5! = 5160. A
17 min read
Move all zeros to start and ones to end in an Array of random integers
Given an array arr[] of random integers, the task is to push all the zero's in the array to the start and all the one's to the end of the array. Note that the order of all the other elements should be the same.Example: Input: arr[] = {1, 2, 0, 4, 3, 0, 5, 0} Output: 0 0 0 2 4 3 5 1 Input: arr[] = {1, 2, 0, 0, 0, 3, 6}; Output: 0 0 0 2 3 6 1 Approac
10 min read
Trailing number of 0s in product of two factorials
Given two integer N or M find the number of zero's trailing in product of factorials (N!*M!)? Examples: Input : N = 4, M = 5 Output : 1 Explanation : 4! = 24, 5! = 120 Product has only 1 trailing 0. Input : N = 127!, M = 57! Output : 44 As discussed in number of zeros in N! can be calculated by recursively dividing N by 5 and adding up the quotient
5 min read
Queries for the product of first N factorials
Given Q[] queries where each query consists of an integer N, the task is to find the product of first N factorials for each of the query. Since the result could be large, compute it modulo 109 + 7.Examples: Input: Q[] = {4, 5} Output: 288 34560 Query 1: 1! * 2! * 3! * 4! = 1 * 2 * 6 * 24 = 288 Query 2: 1! * 2! * 3! * 4! * 5! = 1 * 2 * 6 * 24 * 120
7 min read
Print factorials of a range in right aligned format
Given two numbers m and n, the task is to find factorial of all numbers including m and n, and then print below format.Examples: Input : 6 10 Output : 720 5040 40320 362880 3628800 Input : 10 20 Output : 3628800 39916800 479001600 6227020800 87178291200 1307674368000 20922789888000 355687428096000 6402373705728000 121645100408832000 243290200817664
9 min read
Product of first N factorials
Given a number N. Find the product of first N factorials modulo 1000000007. Constraints: 1 ≤ N ≤ 1e6 Examples: Input : 3 Output : 12 Explanation: 1! * 2! * 3! = 12 mod (1e9 + 7) = 12 Input : 5 Output : 34560 Prerequisites: Modular MultiplicationApproach: The basic idea behind solving this problem is to just consider the problem of overflow during t
9 min read
Find the unit place digit of sum of N factorials
Given a number N, the task is to find units place digit of the first N natural numbers factorials, i.e. 1!+2!+3!+....N! where N&lt;=10e18.Examples: Input: n = 2 Output: 31! + 2! = 3Last digit is 3Input: n = 3Output: 91! + 2! + 3! = 9Last digit is 9 Brute Force Approach: In this approach, we are calculating the factorial of each number and then addi
10 min read