Created
November 14, 2016 21:23
-
-
Save devteampentagon/97d9f7af52158817e5871816e5cdf8de to your computer and use it in GitHub Desktop.
Factorial Trailing Zeros
This file contains hidden or 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
| #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