39
\$\begingroup\$

Write a program or function that finds the number of zeroes at the end of n! in base 10, where n is an input number (in any desired format).

It can be assumed that n is a positive integer, meaning that n! is also an integer. There are no zeroes after a decimal point in n!. Also, it can be assumed that your programming language can handle the value of n and n!.


Test cases

1
==> 0

5
==> 1

100
==> 24

666
==> 165

2016
==> 502

1234567891011121314151617181920
==> 308641972752780328537904295461

This is code golf. Standard rules apply. The shortest code in bytes wins.

\$\endgroup\$
10
  • \$\begingroup\$ Related. \$\endgroup\$
    – xnor
    May 12, 2016 at 2:39
  • \$\begingroup\$ Can we assume that n! will fit within our languages' native integer type? \$\endgroup\$
    – Alex A.
    May 12, 2016 at 2:45
  • \$\begingroup\$ @AlexA. Yes you can. \$\endgroup\$
    – Arcturus
    May 12, 2016 at 3:01
  • 17
    \$\begingroup\$ I think this would be a better question if you were not allowed to assume n! would fit into your integer type! Well, maybe another time. \$\endgroup\$
    – A Simmons
    May 12, 2016 at 10:45
  • 1
    \$\begingroup\$ @ASimmons Most of the answers so far, or at least the ones that use the floor division trick, don't rely on that assumption anyway. \$\endgroup\$
    – Alex A.
    May 12, 2016 at 16:53

61 Answers 61

1 2
3
0
\$\begingroup\$

Risky, 6 bytes

!!?+_0__/\\?

Try it online!

Explanation

! length
!       factorial
?         input
+     +
_
0         0
_   apply function
_
/         5
\     find indices in
\       prime factors
?         argument
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.