diff --git a/python/p034.py b/python/p034.py index 6a97c57f9a0c15bcfe9196d95d7b5830201e28d2..ddee314ef9e3ed2692da5c554ad162ea9b19f20d 100644 --- a/python/p034.py +++ b/python/p034.py @@ -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__": diff --git a/python/p070.py b/python/p070.py index a4354d2a69c9d71b886d6cfd22904c4930b80480..eaa90f9ba0b3f57b52a8564c0d65e470d4f76020 100644 --- a/python/p070.py +++ b/python/p070.py @@ -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) diff --git a/python/p092.py b/python/p092.py index fe442cc23df77eb67379f826c5348394d227e42e..765e96183064ddffdf057c520d73a8c869ea5672 100644 --- a/python/p092.py +++ b/python/p092.py @@ -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())