Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active August 20, 2016 13:04
Show Gist options
  • Save rishi93/6b335d13327c061eb85a to your computer and use it in GitHub Desktop.
Save rishi93/6b335d13327c061eb85a to your computer and use it in GitHub Desktop.
Factorial Trailing Zeros - FCTRL
#include <iostream>
using namespace std;
int noOfTrailingZeros(int n){
int count = 0;
int divisor = 5;
//First, we count the number of multiples of 5 in the expansion of n!
//Then we count the number of multiples of 25, and then 125, and so on..
while(divisor <= n){
count += n/divisor;
divisor *= 5;
}
return count;
}
int main(){
//For Fast IO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,n;
cin>>t;
for(int testcase=0; testcase<t; testcase++){
cin>>n;
cout<<noOfTrailingZeros(n)<<"\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment