Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created May 29, 2020 03:25
Show Gist options
  • Save Ch-sriram/398355f7ab6ee6ef52c49cab554365ef to your computer and use it in GitHub Desktop.
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!
// 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