euler/python/e104.py

38 lines
849 B
Python
Raw Normal View History

2021-06-25 18:44:37 +02:00
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)