forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol1.py
141 lines (102 loc) · 2.9 KB
/
sol1.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Project Euler Problem 104 : https://projecteuler.net/problem=104
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.
It turns out that F541, which contains 113 digits, is the first Fibonacci number
for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9,
but not necessarily in order). And F2749, which contains 575 digits, is the first
Fibonacci number for which the first nine digits are 1-9 pandigital.
Given that Fk is the first Fibonacci number for which the first nine digits AND
the last nine digits are 1-9 pandigital, find k.
"""
import sys
sys.set_int_max_str_digits(0)
def check(number: int) -> bool:
"""
Takes a number and checks if it is pandigital both from start and end
>>> check(123456789987654321)
True
>>> check(120000987654321)
False
>>> check(1234567895765677987654321)
True
"""
check_last = [0] * 11
check_front = [0] * 11
# mark last 9 numbers
for _ in range(9):
check_last[int(number % 10)] = 1
number = number // 10
# flag
f = True
# check last 9 numbers for pandigitality
for x in range(9):
if not check_last[x + 1]:
f = False
if not f:
return f
# mark first 9 numbers
number = int(str(number)[:9])
for _ in range(9):
check_front[int(number % 10)] = 1
number = number // 10
# check first 9 numbers for pandigitality
for x in range(9):
if not check_front[x + 1]:
f = False
return f
def check1(number: int) -> bool:
"""
Takes a number and checks if it is pandigital from END
>>> check1(123456789987654321)
True
>>> check1(120000987654321)
True
>>> check1(12345678957656779870004321)
False
"""
check_last = [0] * 11
# mark last 9 numbers
for _ in range(9):
check_last[int(number % 10)] = 1
number = number // 10
# flag
f = True
# check last 9 numbers for pandigitality
for x in range(9):
if not check_last[x + 1]:
f = False
return f
def solution() -> int:
"""
Outputs the answer is the least Fibonacci number pandigital from both sides.
>>> solution()
329468
"""
a = 1
b = 1
c = 2
# temporary Fibonacci numbers
a1 = 1
b1 = 1
c1 = 2
# temporary Fibonacci numbers mod 1e9
# mod m=1e9, done for fast optimisation
tocheck = [0] * 1000000
m = 1000000000
for x in range(1000000):
c1 = (a1 + b1) % m
a1 = b1 % m
b1 = c1 % m
if check1(b1):
tocheck[x + 3] = 1
for x in range(1000000):
c = a + b
a = b
b = c
# perform check only if in tocheck
if tocheck[x + 3] and check(b):
return x + 3 # first 2 already done
return -1
if __name__ == "__main__":
print(f"{solution() = }")