Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
Last active February 13, 2017 08:32
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 M-ZubairAhmed/219e7676a3b7352d4f2d4ef363e15534 to your computer and use it in GitHub Desktop.
Save M-ZubairAhmed/219e7676a3b7352d4f2d4ef363e15534 to your computer and use it in GitHub Desktop.
Calculating a factorials trailing zeroes
/* When the given problem is to find number number of trailing zeros of the factorial, one is not required to
find out the factorial.
Statement : factorial(5) = 120 = 1*2*3*4*5
factorial(4) = 24 = 1*2*3*4
if it is observed the occurance of 5 or multiples of five is in direct proportion to number of trailing zeros
If however, expression contains powers of 5, then the trailing zeros increasing with power of 5.
factorial(25) = 1*...5*..10..*15*..20*..25
As expected number of trailing zero in above is expected to be 5, but since 25 is 5 power 2, it adds another zero
hence final count is 6.
*/
int count = 0;
if (fact <= 4){
return 0;}
else if (fact == 5){
return 1;}
else {
for (int i = 5; fact/i >= 1; i*=5) {
count = count + (fact/i);}
return count;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment