Trailing zeros of 100!

Peter Otten __peter__ at web.de
Sat Jan 2 12:18:05 EST 2016


Robin Koch wrote:

> Am 02.01.2016 um 12:49 schrieb katye2007 at gmail.com:
> 
>> I'm trying to write a python program to find how many trailing zeros
>> are in 100! (factorial of 100). I used factorial from the math
>> module, but my efforts to continue failed. Please help.
> 
> Using not Python, but math:
> 
> Every "0" at the end of 100! represents a multiplication by the factor
> 10 or the factors 2 and 5.
> 
> There are 20 numbers with at least one factor 5.
> There are  4 numbers with at least two factors 5 (=25).
> There are no numbers with three factors 5 (=125).
> 
> There are 50 numbers with at least one factor 2.
> 
> So 100! contains 24 factors 5 and even more factors 2.
> So 100! contains 24 facotrs 10 and therefore has 24 trailing zeros.

Spelt in Python:

>>> def trailing_zeros_fact(n):
...     total = 0
...     while n:
...         n //= 5
...         total += n
...     return total
... 
>>> trailing_zeros_fact(100)
24
>>> trailing_zeros_fact(math.factorial(100))
23331553860986038170424809714066675122678992066095405367148240973804399998307478902235365994039129571563424480206805939562796302729215999999999999999999999901





More information about the Python-list mailing list