-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem104.py
77 lines (64 loc) · 1.07 KB
/
problem104.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Pandigital Fibonacci ends
# Problem 104
from collections import Counter
from itertools import izip
def check(l):
c = Counter(l)
if 0 in c or len(c) != 9:
return False
for i in c:
if c[i] != 1:
return False
return True
def pandigital_last(n):
l = []
for i in xrange(9):
l.append(n % 10)
n /= 10
return check(l)
def pandigital_first(n):
while n >= 1000000000:
n /= 10
return pandigital_last(n)
def fibonacci(f=lambda x:x):
fn_1, fn_2 = 1, 1
yield 1
while True:
yield fn_1
fn_1, fn_2 = f(fn_1 + fn_2), fn_1
def pandigital(n):
l = []
while n:
l.append(n % 10)
n /= 10
return check(l)
def first9(n):
while n >= 1000000000:
n /= 10
return n
def last9(n):
return n % 1000000000
def main():
limit = 500000
i = 1
l = []
for f_last in fibonacci(last9):
p_last = pandigital(f_last)
if p_last:
l.append(i)
if i > limit:
break
i += 1
i = 1
j = 0
for fn in fibonacci():
if j >= len(l) or i > limit:
break
if i == l[j]:
if pandigital_first(fn):
print i
break
j += 1
i += 1
if __name__ == '__main__':
main()