This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[61]CyclicalFigurateNumbers.py
More file actions
107 lines (102 loc) · 4.77 KB
/
[61]CyclicalFigurateNumbers.py
File metadata and controls
107 lines (102 loc) · 4.77 KB
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
"""
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:
Triangle P3,n=n(n+1)/2 1, 3, 6, 10, 15, ...
Square P4,n=n^2 1, 4, 9, 16, 25, ...
Pentagonal P5,n=n(3n−1)/2 1, 5, 12, 22, 35, ...
Hexagonal P6,n=n(2n−1) 1, 6, 15, 28, 45, ...
Heptagonal P7,n=n(5n−3)/2 1, 7, 18, 34, 55, ...
Octagonal P8,n=n(3n−2) 1, 8, 21, 40, 65, ...
The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.
The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).
Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882), is represented by a different number in the set.
This is the only set of 4-digit numbers with this property.
Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.
"""
triangles = list(filter(lambda x : len(str(x)) == 4, [int(n * (n + 1) / 2) for n in range(150)]))
squares = list(filter(lambda x : len(str(x)) == 4, [int(n ** 2) for n in range(150)]))
pentagonals = list(filter(lambda x : len(str(x)) == 4, [int(n * (3 * n - 1) / 2) for n in range(150)]))
hexagonals = list(filter(lambda x : len(str(x)) == 4, [int(n * (2 * n - 1)) for n in range(100)]))
heptagonals = list(filter(lambda x : len(str(x)) == 4, [int(n * (5 * n - 3) / 2) for n in range(100)]))
octogonals = list(filter(lambda x : len(str(x)) == 4, [int(n * (3 * n - 2)) for n in range(100)]))
tried = set()
done = ["tri"] #keeps track of which types we have already so we don't have duplicates
is_match = lambda x, y : str(x)[2:] == str(y)[:2]
def find_match(n, sequence):
if not len(sequence):
done.clear()
return False, []
if len(sequence) == 6:
return is_match(n, sequence[0]), sequence
elif len(list(filter(lambda x : is_match(n, x) and x not in tried, squares))) and "sq" not in done: #squares
done.append("sq")
m = list(filter(lambda x : is_match(n, x) and x not in tried, squares))[0]
sequence.append(m)
match, s = find_match(m, sequence)
if match:
return match, sequence
else:
sequence.pop()
done.pop()
tried.add(m)
return find_match(n, sequence)
elif len(list(filter(lambda x : is_match(n, x) and x not in tried, pentagonals))) and "pent" not in done: #pentagonals
done.append("pent")
m = list(filter(lambda x : (is_match(n, x) and x not in tried), pentagonals))[0]
sequence.append(m)
match, s = find_match(m, sequence)
if match:
return match, sequence
else:
sequence.pop()
done.pop()
tried.add(m)
return find_match(n, sequence)
elif len(list(filter(lambda x : is_match(n, x) and x not in tried, hexagonals))) and "hex" not in done: #hexagonals
done.append("hex")
m = list(filter(lambda x : is_match(n, x) and x not in tried, hexagonals))[0]
sequence.append(m)
match, s = find_match(m, sequence)
if match:
return match, sequence
else:
sequence.pop()
done.pop()
tried.add(m)
return find_match(n, sequence)
elif len(list(filter(lambda x : is_match(n, x) and x not in tried, heptagonals))) and "hept" not in done: #heptagonals
done.append("hept")
m = list(filter(lambda x : is_match(n, x) and x not in tried, heptagonals))[0]
sequence.append(m)
match, s = find_match(m, sequence)
if match:
return match, sequence
else:
sequence.pop()
done.pop()
tried.add(m)
return find_match(n, sequence)
elif len(list(filter(lambda x : is_match(n, x) and x not in tried, octogonals))) and "oct" not in done: #octogonals
done.append("oct")
m = list(filter(lambda x : is_match(n, x) and x not in tried, octogonals))[0]
sequence.append(m)
match, s = find_match(m, sequence)
if match:
return match, sequence
else:
sequence.pop()
done.pop()
tried.add(m)
return find_match(n, sequence)
else:
return False, sequence
#print(list(filter(lambda x : is_match(8128, x), pentagonals)))
for t in triangles:
tried.clear()
sequence = [t] #keeps track of which ones are "in our cart"
done = ["tri"]
found, sequence = find_match(t, sequence)
if found:
print(sum(sequence))
break
elif t == triangles[-1]:
print("NONE")