-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_061.py
82 lines (60 loc) · 3.47 KB
/
problem_061.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
# -*- coding: utf-8 -*-
'''
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.
'''
def test_is_four_digit():
assert is_four_digit(1234) is True
assert is_four_digit(123) is False
assert is_four_digit(99999) is False
def is_four_digit(number):
return len(str(number)) == 4
def test():
test_is_four_digit()
test_is_cyclic()
def test_is_cyclic():
assert is_cyclic(1232, 3214) is True
assert is_cyclic(1234, 5678) is False
def is_cyclic(number1, number2):
return str(number1)[-2:] == str(number2)[:2]
def generate_number(method):
factories = {
"triangle": lambda n: n * (n + 1) // 2,
"square": lambda n: n ** 2,
"pentagonal": lambda n: n * (3 * n - 1) // 2,
"hexagonal": lambda n: n * (2 * n - 1),
"heptagonal": lambda n: n * (5 * n - 3) // 2,
"octagonal": lambda n: n * (3 * n - 2),
}
return factories[method]
figurates = {
"triangles": [el for el in map(generate_number("triangle"), range(200)) if is_four_digit(el)],
"squares": [el for el in map(generate_number("square"), range(100)) if is_four_digit(el)],
"pentagonals": [el for el in map(generate_number("pentagonal"), range(100)) if is_four_digit(el)],
"hexagonals": [el for el in map(generate_number('hexagonal'), range(71)) if is_four_digit(el)],
"heptagonals": [el for el in map(generate_number("heptagonal"), range(71)) if is_four_digit(el)],
"octagonals": [el for el in map(generate_number("octagonal"), range(70)) if is_four_digit(el)],
}
def main():
all_figurates = [(key, value) for key in figurates.keys() for value in figurates[key]]
for k1, v1 in all_figurates:
for k2, v2 in [(k, v) for k, v in all_figurates if k not in [k1] and is_cyclic(v1, v)]:
for k3, v3 in [(k, v) for k, v in all_figurates if k not in [k1, k2] and is_cyclic(v2, v)]:
for k4, v4 in [(k, v) for k, v in all_figurates if k not in [k1, k2, k3] and is_cyclic(v3, v)]:
for k5, v5 in [(k, v) for k, v in all_figurates if k not in [k1, k2, k3, k4] and is_cyclic(v4, v)]:
for k6, v6 in [(k, v) for k, v in all_figurates if k not in [k1, k2, k3, k4, k5] and is_cyclic(v5, v)]:
if is_cyclic(v6, v1):
return sum([v1, v2, v3, v4, v5, v6])
if __name__ == "__main__":
test()
print(main()) # 28684 in 214 msec