Created
May 29, 2020 03:25
-
-
Save Ch-sriram/398355f7ab6ee6ef52c49cab554365ef to your computer and use it in GitHub Desktop.
Given a number N, find the number of trailing zeroes in N!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Good explanation: https://www.purplemath.com/modules/factzero.htm | |
#include <bits/stdc++.h> | |
using namespace std; | |
uint64_t trailing_zeroes(uint64_t n) { | |
uint64_t zeroes = 0; | |
for(uint64_t i = 1ULL, factor = 5ULL; factor <= n; ++i, factor = pow(5, i)) | |
zeroes += (n / factor); | |
return zeroes; | |
} | |
int main() { | |
int t; cin >> t; | |
while(t--) { | |
uint64_t n; cin >> n; | |
cout << trailing_zeroes(n) << "\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment