Open In App

CSES Solutions – Trailing Zeros

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Your task is to calculate the number of trailing zeros in the factorial N!.

Examples:

Input: N = 20
Output: 4
Explanation: 20! = 2432902008176640000 and it has 4 trailing zeros.

Input: N = 6
Output: 1
Explanation: 6! = 720 and it has 1 trailing zero.

Approach: To solve the problem, follow the below idea:

If we observe carefully, the number of trailing zeros in N! is same as calculating the number of times the number N! is divisible by 10. We can find this by finding the number of pairs of {2, 5} in the prime factorization of N! as 2 * 5 = 10. Since the number of 2s will always be greater than the number of 5s in prime factorization of N!, therefore we can only calculate the number of 5s to calculate the total number of trailing zeros.

Also, the number of 5s in the prime factorization can be different for different numbers. Every multiple of 5 will have at least one 5 in the prime factorization. Similarly, every multiple of 25 will have at least two 5s and every multiple of 125 will have at least three 5s in the prime factorization as so on. So, we can first count the number of 5s for every multiple of 5 then for every multiple of 25, then 125 and so on to calculate the final answer.

Step-by-step algorithm:

  • Maintain a function solve(N) to recursively count the number of occurrences of 5 till N.
  • Check if N == 0, then return 0.
  • Otherwise, return N / 5 + solve(N / 5).

Below is the implementation of the algorithm:

C++
#include <iostream>
using namespace std;

// Recursive function to calculate the multiples of 5 till N
int solve(int N)
{
    if (N == 0) {
        return 0;
    }
    return N / 5 + solve(N / 5);
}

int main()
{
    int N = 20;
    cout << solve(N) << "\n";
    return 0;
}
Java
import java.util.*;

public class Main {

    // Recursive function to calculate the multiples of 5 till N
    static int solve(int N) {
        if (N == 0) {
            return 0;
        }
        return N / 5 + solve(N / 5);
    }

    public static void main(String[] args) {
        int N = 20;
        System.out.println(solve(N));
    }
}

// This code is contributed by rambabuguphka
C#
using System;

public class GFG
{
    // Recursive function to calculate the multiples of the 5 till N
    static int Solve(int N)
    {
        // Base case: If N is 0
        // return 0
        if (N == 0)
        {
            return 0;
        }
        // Recursively calculate the multiples of the 5 and add to the result
        return N / 5 + Solve(N / 5);
    }
    public static void Main(string[] args)
    {
        int N = 20;
        // Call the Solve function and print the result
        Console.WriteLine(Solve(N));
    }
}
JavaScript
// Recursive function to calculate the multiples of 5 till N
function solve(N) {
    if (N === 0) {
        return 0;
    }
    return Math.floor(N / 5) + solve(Math.floor(N / 5));
}

function main() {
    const N = 20;
    console.log(solve(N));
}

main();
Python3
# Recursive function to calculate the multiples of 5 till N
def solve(N):
    if N == 0:
        return 0
    return N // 5 + solve(N // 5)

def main():
    N = 20
    print(solve(N))

if __name__ == "__main__":
    main()

Output
4

Time Complexity: O(log5N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads