-
Notifications
You must be signed in to change notification settings - Fork 0
/
p31-40.py
268 lines (200 loc) · 5.9 KB
/
p31-40.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from math import *
from time import time
from math_utils import list_is_prime
def p31(wanted=200, coins=(200, 100, 50, 20, 10, 5, 2, 1)):
if len(coins) == 1:
return wanted >= 0 and wanted % coins[0] == 0
num_of_choices = 0
for n in range(int(wanted // coins[0]) + 1):
num_of_choices += p31(wanted - n * coins[0], coins[1:])
return num_of_choices
def p32():
min_of, max_of, sum_of = 1000, 10000, 0
counted = [False] * max_of
for i in range(1, min_of):
for j in range(min_of // i, max_of // i):
dig = [0] * 10
a, b, c = i, j, i * j
while a:
dig[a % 10] += 1
a //= 10
while b:
dig[b % 10] += 1
b //= 10
while c:
dig[c % 10] += 1
c //= 10
pandigital = dig[0] == 0
for digit in dig[1:]:
if digit != 1:
pandigital = False
if pandigital:
if not counted[i * j]:
sum_of += i * j
counted[i * j] = True
return sum_of
def gcd(m, n):
return gcd(n, m % n) if n else m
def list_primes(n):
if n <= 2:
return [] if n - 2 else [2]
primes = [2, 3]
for y in range(5, n + 1, 6):
for x in [y, y + 2] if y + 2 <= n else [y]:
square = int(sqrt(x))
for p in primes:
if p > square:
primes += [x]
break
if x % p == 0:
break
return primes
def p33():
x, y = 1, 1
for a in range(11, 100):
for b in range(a + 1, 100):
if a % 10 and a % 10 == b // 10:
if (a // 10) * b == a * (b % 10):
x, y = a * x, b * y
return y // gcd(x, y)
def p35():
n = 1000000
(bool_primes, primes), have_been = list_is_prime(n, True), [False] * (n + 1)
val, valc, counter = 1, 0, 3
if n <= 5:
return sum(bool_primes.values())
for p in primes[counter:]: # 2,3,5 already counted
if not have_been[p]:
if p > val:
val *= 10
valc += 1
s, proceed, num = str(p), True, p
while num:
if not num % 2 and num % 5:
proceed = False
break
num //= 10
temp_counter = 1
have_been[p] = True
for i in range(valc):
s = s[1:] + s[0]
if not bool_primes[int(s)]:
proceed = False
break
temp_counter += not have_been[int(s)]
have_been[int(s)] = True
if proceed:
counter += temp_counter
return counter
def is_prime(x):
if x <= 3:
return x >= 2
if x % 2 == 0 or x % 3 == 0:
return False
for p in range(5, int(sqrt(x)) + 1, 6):
if x % p == 0 or x % (p + 2) == 0:
return False
return True
def p35s():
n = 1000000
have_been = [False] * (n + 1)
val, valc, counter = 1, 0, 4
if n <= 7:
return 0 if n <= 1 else (n + 1) // 2
for p in range(11, n + 1, 2): # 2,3,5 already counted
if not have_been[p]:
if not is_prime(p):
continue
if p > val:
val *= 10
valc += 1
s, proceed, num = str(p), True, p
while num:
if num % 2 == 0 or num % 5 == 0:
proceed = False
break
num //= 10
temp_counter = 1
for i in range(1, valc):
p = int(s[1:] + s[0])
s = str(p)
if not proceed or not is_prime(p):
proceed = False
temp_counter += not have_been[p]
have_been[p] = True
if proceed:
counter += temp_counter
return counter
def is_palindrome(s):
length = len(s)
for i in range(length // 2):
if s[i] != s[length - 1 - i]:
return False
return True
def p36():
counter = 0
for x in range(1000000):
if is_palindrome(str(x)) and is_palindrome(bin(x)[2:]):
counter += x
return counter
def p37():
counter, sum_of, x = 0, 0, 10
primes = list_is_prime(x - 1)
while counter < 11:
if is_prime(x):
primes += [1]
a, b = x // 10, str(x)[1:]
proceed = True
while proceed and a:
if not primes[a]:
proceed = False
a //= 10
while proceed and b:
if not primes[int(b)]:
proceed = False
b = str(b)[1:]
if proceed:
counter += 1
sum_of += x
else:
primes += [0]
x += 1
return sum_of
def p38():
max_pan, wanted = 123456789, sorted('123456789')
for x in range(2, 10000):
s, n = '', 1
while len(s) < 9:
s += str(x * n)
n += 1
if sorted(s) == wanted:
pan = int(s)
if pan > max_pan:
max_pan = pan
return max_pan
def p39():
max_p, max_sol, n = 0, 0, 1000
for p in range(n + 1):
sols = 0
for c in range(5, p // 2):
delta = 4 * c ** 2 + 8 * p * c - 4 * p ** 2
if delta >= 0 and sqrt(delta) == int(sqrt(delta)):
sols += 1
if sols > max_sol:
max_sol = sols
max_p = p
return max_p
def p40():
d = ''
for x in range(500005):
d += str(x)
mul = 1
for n in range(7):
mul *= ord(d[10 ** n]) - ord('0')
return mul
def timed_run():
start = time()
print(p40())
print(f"{time() - start:0.2f}s")
if __name__ == '__main__':
timed_run()