Trailing Zeroes in Factorial

Mr. Yash asks all the great programmers of the world to solve a trivial problem. He gives them an integer m and asks for the number of positive integers n, such that the factorial of n ends with exactly m zeroes. Are you among those great programmers who can solve this problem?

Example:

Input:  m = 1

Output:

5 5 6 7 8 9

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

void trailingZerosInFactorial(long long m)
{
    long long nkc;

    long long z = m * 5;
    while (true)
    {
        c = 0;
        n = z;
        while (true)
        {
            k = n / 5;
            if (k == 0)
                break;
            c += k;
            n = n / 5;
        }
        if (c <= m)
            break;
        z = z - 5;
    }
    if (c == m)
    {
        cout << 5 << "\n";
        for (long long i = zi < z + 5i++)
            cout << i << " ";
    }
    else
        cout << "0";
    cout << "\n";
}
int main()
{

    long long m = 1;

    trailingZerosInFactorial(m);

    return 0;
}


No comments:

Post a Comment