# Factorial question: number of trailing zeroes in 125! [duplicate]

How many zeros are after the last nonzero digit of 125! ?

The answer is 31, but how do you solve it?

-

## marked as duplicate by m_t_, Jeel Shah, Clarinetist, Ross Millikan, Benjamin DickmanFeb 22 at 18:21

The question is asking you to find the highest power of ten dividing $$125! = 125 \cdot 124 \cdot 123 \cdot \cdots \cdot 2 \cdot 1.$$ So count how many copies of $2$ and $5$ there are, then take the minimum. – Ethan Alwaise Feb 22 at 4:39
You can find several similar questions on this site. See Derive a formula to find the number of trailing zeroes in $n!$ and maybe some other questions tagged decimal-expansion+factorial – Martin Sleziak Feb 22 at 13:15

Well, we know that to have a zero at the end then $10$ must be a factor, which means $5$ and $2$ must be factors. However, every other factor is even, so there are far more factors of $2$ than $5$ - As such, we have to count the number of factors divisible by $5$. The number of factors divisible by $5$ less than or equal to $125$ is $25$ (we just do $\frac{125}{5}$), so the answer appears to be $25$, but then we remember that $25 = 5\cdot5$, so we must count double for each factor divisible by $25$, of which there are $5$; accordingly, our new answer is $30$. BUT WAIT... $125 = 5^3$, so we have to count it too, giving us a final answer of $31$

-

The formula explaining above: $$\text{Number of zeros in }n!=\left[ \frac {n}{5} \right]+\left[ \frac {n}{5^2} \right]+\ldots$$ where [.] denotes the greatest integer function.

-

Note that if there is at least one zero after the last non-zero digit of $125!$, then $125!$ is divisible by $10$. Likewise, if there are two, then $125!$ is divisible by $100$, etc.. Thus, we need to check how many times $125!$ is divisible by $10$.

So, we count the multiples of $5^1$, $5^2$, and $5^3 = 125$, in $125!$. It is easy to see that there are $25 = 125/5$ factors divisible by $5^1 = 5$, less than $125$. Similarly, there are $5 = 125/25$ factors divisible by $5^2 = 25$ in $125$. And finally, there is $1 = 125/125$ factors divisible by $5^3 = 125$. Thus, by the sum rule, there are $25 + 5 + 1 = 31$ such factors. But, this counts the number of times $125!$ is divisible by $10$, since $10 = 5 \times 2$, and there is a $2$ in the prime decomposition of $125!$.

-

I just count them.

125! = 188267717688892609974376770249160085759540364871492425887598231508353156331613598866882932889495923133646405445930057740630161919341380597818883457558547055524326375565007131770880000000000000000000000000000000

Have a Apple computer? Compute the value yourself. Below is the applescript. Paste into the Script Editor application and click on the "Run the Script" button.

on ids(digits as text)
if digits is "" or digits is not "0" and digits starts with "0" then error "bad integer " & digits
set x to (id of digits) as list
repeat with digit in x
if digit < 48 or digit > 57 then error "bad integer"
end repeat
return x
end ids

on add(x as text, y as text)
set {a, b, n, carry} to {ids(x), ids(y), (length of x) - (length of y), 0}
if n < 0 then set {a, b, n} to {b, a, -n}
repeat with i from length of a to 1 by -1
set {carry, sum, j} to {0, carry + (item i of a), i - n}
if j > 0 then set sum to sum + (item j of b) - 48
if sum > 57 then set {carry, sum} to {1, sum - 10}
set item i of a to sum
end repeat
if carry > 0 then set beginning of a to carry + 48
return character id a

on multiply(x as text, y as text)
set {a, b, c, sum} to {ids(x), ids(y), {}, "0"}
repeat with m from length of b to 1 by -1
set multiplier to (item m of b) - 48
if multiplier is not 0 and x is not "0" then
set carry to 0
repeat with i from length of a to 1 by -1
set product to carry + ((item i of a) - 48) * multiplier
set carry to product div 10
set beginning of c to product mod 10 + 48
end repeat
if carry > 0 then set beginning of c to carry + 48
set sum to add(sum, character id c)
end if
set end of c to 48
set c to items (m - (length of b) - 1) thru end of c
end repeat
return sum
end multiply

set x to 1
repeat with y from 1 to 125
set x to multiply(x, y)
end repeat

-
Python: import math; digits = str(math.factorial(125)); print(len(digits) - len(digits.rstrip('0'))). – RemcoGerlich Feb 22 at 17:56
Could you run that script and tell us the number of trailing zeroes in $125000!$? – Barry Cipra Feb 22 at 17:57