Commit 1dc03886 authored by Nayuki Minase's avatar Nayuki Minase

P34, P70, P92: Optimized Python solutions.

parent bd756ea1
......@@ -17,14 +17,15 @@ def compute():
return str(ans)
FACTORIAL = list(map(eulerlib.factorial, range(10)))
def factorial_digit_sum(n):
result = 0
while n > 0:
result += FACTORIAL[n % 10]
n //= 10
return result
while n >= 10000:
result += FACTORIAL_DIGITS_SUM_WITH_LEADING_ZEROS[n % 10000]
n //= 10000
return result + FACTORIAL_DIGITS_SUM_WITHOUT_LEADING_ZEROS[n]
FACTORIAL_DIGITS_SUM_WITHOUT_LEADING_ZEROS = [sum(eulerlib.factorial(int(c)) for c in str(i)) for i in range(10000)]
FACTORIAL_DIGITS_SUM_WITH_LEADING_ZEROS = [sum(eulerlib.factorial(int(c)) for c in str(i).zfill(4)) for i in range(10000)]
if __name__ == "__main__":
......
......@@ -17,7 +17,7 @@ def compute():
minnumer = 1
mindenom = 0
for (i, tot) in enumerate(totients[2 : ], 2):
if sorted(str(i)) == sorted(str(tot)) and i * mindenom < minnumer * tot:
if i * mindenom < minnumer * tot and sorted(str(i)) == sorted(str(tot)):
minnumer = i
mindenom = totients[i]
return str(minnumer)
......
......@@ -20,11 +20,12 @@ def compute():
def square_digit_sum(n):
result = 0
while n > 0:
x = n % 10
result += x * x
n //= 10
result += SQUARE_DIGITS_SUM[n % 1000]
n //= 1000
return result
SQUARE_DIGITS_SUM = [sum(int(c)**2 for c in str(i)) for i in range(1000)]
if __name__ == "__main__":
print(compute())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment