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 n, k, c;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 = z; i < z + 5; i++)cout << i << " ";}elsecout << "0";cout << "\n";}int main(){long long m = 1;trailingZerosInFactorial(m);return 0;}
No comments:
Post a Comment