project-euler/160_factorial_trailing_digi...

42 lines
748 B
Python
Raw Normal View History

2023-03-25 17:24:17 +01:00
#!/usr/bin/env python3
from math import log
digs = 5
mask = 10**digs
sieve = [1] * mask
for n in range(1, mask):
if n%2 and n%5:
sieve[n] = (sieve[n-1] * n) % mask
else:
sieve[n] = sieve[n-1]
def numf(n, N):
return sum(N // (n**pw) for pw in range(1, int(log(N) / log(n))+1))
def numof2(N):
return numf(2, N) - numf(5, N)
def roots(N):
a = 1
while a <= N:
b = 1
while a * b <= N:
yield a * b
b *= 2
a *= 5
def main(N):
r = 1
p = sieve[-1]
for root in roots(N):
d = N // root
r *= pow(p, d // mask, mask)
r *= sieve[d % mask]
r %= mask
r *= pow(2, numof2(N), mask)
return r % mask
print(main(10**12))