Skip to content

Instantly share code, notes, and snippets.

@ivycheung1208
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivycheung1208/c10a431f32a91a9f2b6c to your computer and use it in GitHub Desktop.
Save ivycheung1208/c10a431f32a91a9f2b6c to your computer and use it in GitHub Desktop.
CC150 17.3
/* 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