Last active
August 29, 2015 14:05
-
-
Save ivycheung1208/c10a431f32a91a9f2b6c to your computer and use it in GitHub Desktop.
CC150 17.3
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
/* CC150 17.3 */ | |
// compute the number of tailing zeros in n factorial | |
#include <iostream> | |
int tailZeros(int n) | |
{ | |
int count = 0; | |
for (int fact = 5; fact <= n; fact += 5) { | |
for (int j = fact; j % 5 == 0; j /= 5) | |
++count; // increase count for each factor 5 in fact | |
} | |
return count; | |
} | |
int tailZeros2(int n) | |
{ | |
int count = 0; | |
for (int fact = 5; fact <= n; fact *= 5) { // power of 5 | |
for (int j = fact; j <= n; j += fact) // count all multiples of fact | |
++count; | |
} | |
return count; | |
} | |
int main() | |
{ | |
int n; | |
std::cin >> n; | |
std::cout << tailZeros(n) << std::endl; | |
std::cout << tailZeros2(n) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment