-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path061.py
More file actions
147 lines (133 loc) · 5.15 KB
/
061.py
File metadata and controls
147 lines (133 loc) · 5.15 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
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
'''
THIS CODE IS WRONG - LOOK TO THE .C ONE
Problem 61
16 January 2004
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.
1.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).
2.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.
3.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 create_sets():
triang = [n*(n+1)/2 for n in range(1,150) if (n*(n+1)/2 > 999 and n*(n+1)/2 <= 9999) ]
square = [n*n for n in range(1,150) if (n*n > 999 and n*n <= 9999) ]
penta = [n*(3*n-1)/2 for n in range(1,150) if (n*(3*n-1)/2 > 999 and n*(3*n-1)/2 <= 9999) ]
hexa = [n*(2*n-1) for n in range(1,150) if (n*(2*n-1) > 999 and n*(2*n-1) <= 9999) ]
hepta = [n*(5*n-3)/2 for n in range(1,150) if (n*(5*n-3)/2 > 999 and n*(5*n-3)/2 <= 9999) ]
octa = [n*(3*n-2) for n in range(1,150) if (n*(3*n-2) > 999 and n*(3*n-2) <= 9999) ]
return [triang, square, penta, hexa, hepta, octa]
def check(nums,sets):
l1=set([x/100 for x in nums])
l2=set([x-100*(x/100) for x in nums])
if l1==l2:
if check_types(nums,sets): return True
return False
def check_types(nums,sets):
if set(nums) & set(sets[0]) != set() and \
set(nums) & set(sets[1]) != set() and \
set(nums) & set(sets[2]) != set() and \
set(nums) & set(sets[3]) != set() and \
set(nums) & set(sets[4]) != set() and \
set(nums) & set(sets[5]) != set():
return True
return False
from sys import stdout
from time import time
sets=create_sets()
it=len(sets[5])
jt=len(sets[4])
kt=len(sets[3])
intertot=it*jt*kt
print "Octa Hept Hexa"
stt=time()
for i, p5 in enumerate(sets[5]):
for j, p4 in enumerate(sets[4]):
for k, p3 in enumerate(sets[3]):
for p2 in sets[2]:
for p1 in sets[1]:
for p0 in sets[0]:
nums=[p5, p4, p3, p2, p1, p0]
if check(nums,sets):
print nums
et=time()-stt
rt=intertot/(k+1+j*kt+i*jt*kt) * et / 3600
stdout.write("%d %d %d - %.3fh remaining\r" % (p5,p4,p3,rt))
stdout.flush()
'''
Octa Hept Hexa
[1045, 2512, 1225, 4510, 1225, 1225]
[1045, 2512, 1225, 4510, 1225, 5050]
[1045, 2512, 1225, 4510, 1225, 5151]
[1045, 2512, 1225, 4510, 5625, 2556]
[1045, 2512, 2556, 4510, 5625, 1225]
[1045, 2512, 5151, 4510, 1225, 1225]
[1045, 2512, 5151, 4510, 1225, 5050]
[1045, 2512, 5151, 4510, 1225, 5151]
[1045, 2512, 5565, 4510, 1225, 6555]
[1045, 2839, 8128, 4510, 1681, 3916]
[1045, 4141, 2556, 4510, 5625, 2556]
[1045, 4141, 2556, 4510, 5625, 5050]
[1045, 4141, 2556, 4510, 5625, 5151]
[1045, 4141, 5151, 4510, 5041, 5050]
[1045, 4141, 5151, 4510, 5625, 2556]
[1045, 8910, 5151, 4510, 1089, 5050]
[1045, 8910, 5151, 4510, 1089, 5151]
[1045, 8910, 5565, 4510, 1089, 6555]
[1281, 2512, 1225, 2882, 8281, 8128]
[1281, 2512, 8128, 2882, 5625, 8256]
[1281, 2512, 8128, 2882, 8281, 1225]
[2133, 1651, 3321, 1717, 2116, 5151]
[2133, 1651, 3321, 5192, 9216, 3321]
[2133, 1651, 3321, 5192, 9216, 5050]
[2133, 1651, 3321, 5192, 9216, 5151]
[2133, 1651, 5151, 1717, 2116, 3321]
[2133, 1651, 5151, 5192, 9216, 3321]
[2133, 2512, 1225, 1717, 1225, 3321]
[2133, 2512, 3321, 1717, 1225, 1225]
[2133, 2512, 3321, 1717, 1225, 3321]
[2133, 2512, 3321, 1717, 1225, 5050]
[2133, 2512, 3321, 1717, 1225, 5151]
[2133, 2512, 5151, 1717, 1225, 3321]
[2133, 4141, 2556, 1717, 5625, 3321]
[2133, 4141, 3321, 1717, 1764, 6441]
[2133, 4141, 3321, 1717, 3364, 6441]
[2133, 4141, 3321, 1717, 5041, 5050]
[2133, 4141, 3321, 1717, 5625, 2556]
[2133, 4141, 3321, 2882, 8281, 8128]
[2133, 4141, 6441, 1717, 1764, 3321]
[2133, 4141, 6441, 1717, 3364, 3321]
[2133, 4141, 8128, 2882, 8281, 3321]
[2133, 8910, 2145, 4510, 1089, 3321]
[2133, 8910, 3321, 1717, 1089, 3321]
[2133, 8910, 3321, 1717, 1089, 5050]
[2133, 8910, 3321, 1717, 1089, 5151]
[2133, 8910, 3321, 4510, 1089, 2145]
[2133, 8910, 5151, 1717, 1089, 3321]
[2821, 4141, 1128, 1617, 2116, 1711]
[4033, 3367, 1540, 1717, 6724, 2415]
[4033, 3367, 2415, 1717, 6724, 1540]
[4720, 2512, 1225, 1247, 2025, 1225]
[4720, 2512, 1225, 1247, 2025, 5050]
[4720, 2512, 1225, 1247, 2025, 5151]
[4720, 2512, 5151, 1247, 2025, 1225]
[4720, 2512, 5151, 1247, 2025, 5050]
[4720, 2512, 5151, 1247, 2025, 5151]
[4720, 2512, 5565, 1247, 2025, 6555]
[4720, 8037, 5151, 2147, 3721, 2080]
[5985, 2059, 2556, 1520, 5625, 8515]
[8965, 8910, 5151, 5551, 1089, 6555]
[8965, 8910, 5565, 1717, 1089, 6555]
[9633, 3367, 2415, 1717, 6724, 1596]