From 107edad5b114f87843e1be244d3f6eb7ab8e2035 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Fri, 25 Jun 2021 12:44:37 -0400 Subject: [PATCH] Solve problem 104 in Python --- python/e104.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 python/e104.py diff --git a/python/e104.py b/python/e104.py new file mode 100644 index 0000000..f4203d8 --- /dev/null +++ b/python/e104.py @@ -0,0 +1,37 @@ +import math +from itertools import permutations + + +def first_n_digits(num, n): + # https://stackoverflow.com/questions/41271299/how-can-i-get-the-first-two-digits-of-a-number + p = int(math.log(num, 10)) - n + 1 + if p < 0: + return num + return num // 10 ** (int(math.log(num, 10)) - n + 1) + + +def is_end_pandigital(n, ps): + return (n % 10**9) in ps + + +def is_start_pandigital(n, ps): + start = first_n_digits(n, 9) + return start in ps + + +def euler_104(): + a, b = 1, 1 + ps = set([int("".join(map(str, p))) for p in permutations(range(1, 10))]) + k = 3 + while True: + a, b = b, a + b + if is_end_pandigital(b, ps) and is_start_pandigital(b, ps): + return k + k += 1 + + +if __name__ == "__main__": + solution = euler_104() + print("e104.py: " + str(solution)) + assert(solution == 329468) +