5
\$\begingroup\$

Write a function which takes n as a parameter, and returns the number of trailing zeros in n!.

Input Constraints

0 <= n <= 10^100

Output Constraints

Should be return the result in less than 10 seconds.

Test Input

1 213 45678 1234567 78943533 4567894123 121233112233112231233112323123 

Test Output

0 51 11416 308638 19735878 1141973522 30308278058278057808278080759 

Shortest code by character count wins.

\$\endgroup\$
7
  • \$\begingroup\$ I'm not sure if we should have this here, as it's basically the same as spoj.pl/SHORTEN/problems/FACTZERO \$\endgroup\$ Commented Feb 8, 2011 at 15:29
  • \$\begingroup\$ @Nabb, I had no idea that this question was on SPOJ. I've put a significantly larger limit on the input here though. \$\endgroup\$ Commented Feb 8, 2011 at 15:35
  • \$\begingroup\$ @Dogbert: The limit is larger but the algorithms here are going to be exactly the same as over at SPOJ. \$\endgroup\$ Commented Feb 8, 2011 at 16:03
  • \$\begingroup\$ for shortest code, you should use the code-golf tag \$\endgroup\$ Commented Feb 8, 2011 at 19:54
  • \$\begingroup\$ It's on Euler as well :) (i think) \$\endgroup\$ Commented Feb 9, 2011 at 5:50

5 Answers 5

2
\$\begingroup\$

Python 50 47 Characters

n=input() x=5 s=0 while n/x:s+=n/x;x*=5 print s 
\$\endgroup\$
1
\$\begingroup\$

Python, 36 Chars

Ripped from my earlier answer

f=lambda n:n//5+(n//5>0and f(n//5)or 0) #Py3k #39 Chars f=lambda n:n/5+(n/5>0and f(n/5)or 0) #Before 3.0 #36 Chars 
\$\endgroup\$
3
  • \$\begingroup\$ But you still need the input and output right? \$\endgroup\$ Commented Mar 8, 2011 at 23:59
  • \$\begingroup\$ @Debanjan, the question states, write a function.... \$\endgroup\$ Commented Mar 9, 2011 at 5:35
  • \$\begingroup\$ @sOle:My bad!I missed that. \$\endgroup\$ Commented Mar 9, 2011 at 6:05
1
\$\begingroup\$

J, 26

! `f=:3 :'+/<.y%5^(1+i.144x)'` 

eg

 f 121233112233112231233112323123x 30308278058278057808278080759 f 4567894123 1141973522 f 0 0 f 10^100x 2499999999999999...99999999999999999982 

in less than a second for all input examples

\$\endgroup\$
1
\$\begingroup\$

Haskell, 63 bytes

f i=length$filter(\x->x`mod`5/=0)[1..i];main=interact$show.f.read 
\$\endgroup\$
1
\$\begingroup\$

Ruby, 52 43

n=gets.to_i;a=0;i=1 a+=n/i*=5while i<n p a 
\$\endgroup\$
2
  • \$\begingroup\$ You could use (a+=n/i;i*=5)while i<n to save a char. \$\endgroup\$ Commented Feb 8, 2011 at 15:51
  • \$\begingroup\$ And ofcourse, p a as a is a number. \$\endgroup\$ Commented Feb 8, 2011 at 15:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.