-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem61.py
More file actions
115 lines (88 loc) · 3.35 KB
/
problem61.py
File metadata and controls
115 lines (88 loc) · 3.35 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
"""
Cyclical Figurate Numbers
Project Euler Problem #61
by Muaz Siddiqui
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=n2 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.
"""
from euler_helpers import timeit
def figurate_nums(f, start, to):
nums = []
next = 0
n = 1
while next < to:
next = f(n)
if next > start:
nums.append(int(next))
n += 1
nums.pop()
return nums
def remove_invalid(nums):
return [num for num in nums if int(str(num)[-2:]) >= 10]
def triangular(start, to):
return remove_invalid(figurate_nums(lambda x:x*(x+1)/2, start, to))
def square(start, to):
return remove_invalid(figurate_nums(lambda x:x**2, start, to))
def pentagonal(start, to):
return remove_invalid(figurate_nums(lambda x:x*(3*x-1)/2, start, to))
def hexagonal(start, to):
return remove_invalid(figurate_nums(lambda x:x*(2*x-1), start, to))
def heptagonal(start, to):
return remove_invalid(figurate_nums(lambda x:x*(5*x-3)/2, start, to))
def octagonal(start, to):
return remove_invalid(figurate_nums(lambda x:x*(3*x-2), start, to))
def begin(num):
return int(str(num)[:2])
def end(num):
return int(str(num)[-2:])
cycle = [0 for num in range(6)]
nums =[[] for num in range(6)]
def recursive_find(num, length):
for a in range(len(cycle)):
if cycle[a] != 0:
continue
for b in range(len(nums[a])):
distinct = True
for c in range(len(cycle)):
if nums[a][b] == cycle[c]:
distinct = False
break;
if distinct and begin(nums[a][b]) == end(cycle[num]):
cycle[a] = nums[a][b]
if length == 5:
if begin(cycle[5]) == end(cycle[a]):
return True
if recursive_find(a, length + 1):
return True
cycle[a] = 0
return False
@timeit
def answer():
nums[0] = triangular(1000,10000)
nums[1] = square(1000,10000)
nums[2] = pentagonal(1000,10000)
nums[3] = hexagonal(1000,10000)
nums[4] = heptagonal(1000,10000)
nums[5] = octagonal(1000,10000)
for n in range(len(nums[5])):
cycle[5] = nums[5][n]
if recursive_find(5, 1):
break
print(cycle)
return sum(cycle)