-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp061.py
89 lines (69 loc) · 1.92 KB
/
p061.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
"""
Cyclical figurate numbers
https://projecteuler.net/problem=61
"""
from copy import deepcopy
from math import sqrt
def P(p, n):
return (p - 2) * n * (n - 1) // 2 + n
"""
if p == 3:
return n * (n + 1) // 2
elif p == 4:
return n * n
elif p == 5:
return n * (3 * n - 1) // 2
elif p == 6:
return n * (2 * n - 1)
elif p == 7:
return n * (5 * n - 3) // 2
elif p == 8:
return n * (3 * n - 2)
raise
"""
def P_inverse(p, n):
i = int(sqrt(n / (p - 2) * 2))
j = 0
while j < 10:
if P(p, i + j) == n:
return i + j
j += 1
# liste des nombres polygonaux < 10000, le degré est stocké dans un bit
polygonal = [0] * 10000
for n in range(2, 5000):
for p in range(3, 9):
i = P(p, n)
if 1000 <= i < 10000:
polygonal[i] = polygonal[i] | (1 << p)
def cherche(n, masque, liste):
p = polygonal[n]
if p == 0:
return False
# si le masque est le même, la condition d'unicité de représentation
# ne peut plus être respectée
if masque == masque | p:
return False
masque |= p
liste.append(n)
if len(liste) >= 6:
if masque == 0b00111111000:
if liste[0] // 100 == n % 100:
print(sum(liste))
print(liste)
for k in liste:
print(" {} : {}".format(k, ''.join(
["P{}({})".format(i, P_inverse(i, k)) if b == '1' else ""
for i, b in enumerate(reversed(bin(polygonal[k])))])))
return True
return False
# on cycle
n = (n % 100) * 100
# et on cherche un nombre adhoc
for j in range(n + 10, n + 100):
if cherche(j, masque, deepcopy(liste)):
return True
return False
# 2512, 1281, 8128, 2882, 8256, 5625
for i in range(1010, 10000):
if cherche(i, 0, []):
break