Skip to content

Instantly share code, notes, and snippets.

@devteampentagon
Created November 14, 2016 21:23
Show Gist options
  • Select an option

  • Save devteampentagon/97d9f7af52158817e5871816e5cdf8de to your computer and use it in GitHub Desktop.

Select an option

Save devteampentagon/97d9f7af52158817e5871816e5cdf8de to your computer and use it in GitHub Desktop.
Factorial Trailing Zeros
#include <bits/stdc++.h>
#define MEM(a,b) memset((a),(b),sizeof(a))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIn(a,b) ((a)<(b)?(a):(b))
#define MIn4(a,b,c,d) MIn(MIn(MIn(a,b),c),d)
#define In freopen("In.txt", "r", stdin);
#define Out freopen("out.txt", "w", stdout);
#define i64 long long
#define u64 long long unsigned
#define sz 1000000
using namespace std;
// How trailing zeros being calculated?
//Input: 100
// So divide it using 5 repeatedly until 0
// and sum all the results
// 100/5 = 20
// 20/5 = 4
// 4/5 = 0
// so the result: 20+4 = 24
// Means there are 24 zeros from the last of 100!
int trailingZeros(int n)
{
//Calculating Trailing Zeros
//Recursion
if(n==0)
return 0;
return n/5 + trailingZeros(n/5);
}
int main()
{
int n;
while(cin >> n)
cout << trailingZeros(n) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment