X ! X! Has 1000 Trailing Zeros

What is the smallest value of X X , such that X ! X! has 1000 trailing zeros ?


The answer is 4005.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Michael Coley
Oct 23, 2020

This problem was inspired by one of the daily problems .

From the previous problem, we observe that the number of trailing zeros approaches n/4, so a good starting point would be 4000 (although we would expect that to be slightly under 1000).

4000! has 800 multiples of 5; 160 multiples of 25; 32 multiples of 125; 6 multiples of 625; and 3125. As suspected, that's 999 trailing zeros.

4001, 4002, 4003, and 4004 don't add any more 5s.

4005! has 801 multiples of 5; 160 multiples of 25; 32 multiples of 125; 6 multiples of 625; and 3125. That's exactly 1000 trailing zeros.

Hongqi Wang
Oct 23, 2020

i = 1 x 5 i = 1000 x 4 1000 x 4000 \sum\limits_{i=1} \lfloor \frac x{5^i} \rfloor = 1000 \\ \therefore \frac x4 \approx 1000 \Rightarrow x \approx 4000

but i = 1 4000 5 i = 800 + 160 + 32 + 6 + 1 = 999 \sum\limits_{i=1} \lfloor \frac {4000}{5^i} \rfloor = 800 + 160 + 32 + 6 + 1 = 999

So should add one 5 more, i.e. x = 4005 x = 4005

Lu Ca
Oct 24, 2020

I used the Legendre equation. If 1000 traling zeros are needed then X 5 + X 5 2 + X 5 3 + X 5 4 + X 5 5 = 1000 {\huge \left\lfloor \frac{X}{5} \right\rfloor + \left\lfloor \frac{X}{5^2}\right\rfloor + \left\lfloor \frac{X}{5^3}\right\rfloor + \left\lfloor \frac{X}{5^4}\right\rfloor + \left\lfloor \frac{X}{5^5}\right\rfloor = 1000}

X = 1000 3125 781 4001 {\huge X=\frac{1000 \cdot 3125}{781} \cong 4001}

It's near the result because the next 5 multiple 4005 is the correct answer.

Arindam Ghosh
Oct 25, 2020
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from math import *

def countTrailingZero(n):
    count = 0
    n = str(n)
    for i in reversed(n):
        if i == "0":
            count += 1
        else:
            break

    return count

for i in range(0,5000):
    a = countTrailingZero(factorial(i))
    if a == 1000:
        print(i)
    elif a > 1000:
        break

Close. Instead of outputting just the lowest number with 1000 trailing zeros, it outputs all numbers with exactly that many trailing zeros (4005..4009 in this case). If I had picked a number like 5 instead of 1000, your solution wouldn't output anything because 24! has 4 trailing zeros while 25! has 6 trailing zeros. Also, you can speed the program up quite a bit without adding much complexity by calculating the factorial as you loop. I would use something like this for your last block of code:

1
2
3
4
5
6
7
f = 1
for i in range(1,5000):
    f *= i
    a = countTrailingZero(f)
    if a >= 1000:
        print(i)
        break

Michael Coley - 4 months, 2 weeks ago

Log in to reply

nice edit, it would definitely reduce run-time. 👍

Arindam Ghosh - 4 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...