## 嵇爾的吐槽

#没事画轮子的嵇尔不定期的(W)碎(E)碎(B)念(B)和(L)吐(O)槽(G)

Problem 61 Cyclical figurate numbers 2019-07-01 08:30:00

Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:

 Triangle $P_{3,n}=n(n+1)/2$ 1, 3, 6, 10, 15, … Square $P_{4,n}=n2$ 1, 4, 9, 16, 25, … Pentagonal $P_{5,n}=n(3n−1)/2$ 1, 5, 12, 22, 35, … Hexagonal $P_{6,n}=n(2n−1)$ 1, 6, 15, 28, 45, … Heptagonal $P_{7,n}=n(5n−3)/2$ 1, 7, 18, 34, 55, … Octagonal $P_{8,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 ($P_{3,127}=8128$), square ($P_{4,91}=8281$), and pentagonal ($P_{5,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. 一开始因为公式用错了，算出来很多值，都有点怀疑人生了。这个对计算量要求很低的感觉。

import itertools

TRANGLE_INDEX = {}
SQUAEW_INDEX = {}
PENTAGONAL_INDEX = {}
HEXAGONAL_INDEX = {}
HEPTAGONAL_INDEX = {}
OCTAGONAL_INDEX = {}

def triangle(number):
return number*(number+1) / 2

def square(number):
return number*number

def pentagonal(number):
return number*(3*number - 1)/2

def hexagonal(number):
return number*(2*number - 1)

def heptagonal(number):
return number*(5*number - 3) / 2

def octagonal(number):
return number*(3*number - 2)

for i in range(1, 1000):
result = triangle(i)
if result < 1000:
continue
if result > 9999:
break
else:
if not str(result) == '0':
TRANGLE_INDEX[i] = result

for i in range(1, 1000):
result = square(i)
if result < 1000:
continue
if result > 9999:
break
else:
if not str(result) == '0':
SQUAEW_INDEX[i] = result

for i in range(1, 1000):
result = pentagonal(i)
if result < 1000:
continue
if result > 9999:
break
else:
if not str(result) == '0':
PENTAGONAL_INDEX[i] = result

for i in range(1, 1000):
result = hexagonal(i)
if result < 1000:
continue
if result > 9999:
break
else:
if not str(result) == '0':
HEXAGONAL_INDEX[i] = result

for i in range(1, 1000):
result = heptagonal(i)
if result < 1000:
continue
if result > 9999:
break
else:
if not str(result) == '0':
HEPTAGONAL_INDEX[i] = result

for i in range(1, 1000):
result = octagonal(i)
if result < 1000:
continue
if result > 9999:
break
else:
if not str(result) == '0':
OCTAGONAL_INDEX[i] = result

# print TRANGLE_INDEX
# print
# print SQUAEW_INDEX
# print
# print PENTAGONAL_INDEX
# print
# print HEXAGONAL_INDEX
# print
# print HEPTAGONAL_INDEX
# print
# print OCTAGONAL_INDEX
o = itertools.permutations([TRANGLE_INDEX, SQUAEW_INDEX, PENTAGONAL_INDEX,
HEXAGONAL_INDEX, HEPTAGONAL_INDEX, OCTAGONAL_INDEX], 6)
for a, b, c, d, e, f in o:
order = [a, b, c, d, e, f]
for k_1 in order:
v_1 = order[k_1]
v_1_t = str(v_1) + str(v_1)
v_1_h = str(v_1) + str(v_1)
for k_2 in order:
v_2 = order[k_2]
v_2_t = str(v_2) + str(v_2)
v_2_h = str(v_2) + str(v_2)
if v_2_h == v_1_t and not k_1 == k_2:
for k_3 in order:
v_3 = order[k_3]
v_3_t = str(v_3) + str(v_3)
v_3_h = str(v_3) + str(v_3)
if v_3_h == v_2_t and not k_1 == k_3 and not k_2 == k_3:
for k_4 in order:
v_4 = order[k_4]
v_4_t = str(v_4) + str(v_4)
v_4_h = str(v_4) + str(v_4)
if v_4_h == v_3_t and not k_1 == k_4 and not k_2 == k_4 and not k_3 == k_4:
for k_5 in order:
v_5 = order[k_5]
v_5_t = str(v_5) + str(v_5)
v_5_h = str(v_5) + str(v_5)
if v_5_h == v_4_t and not k_1 == k_5 and not k_2 == k_5 and not k_3 == k_5 and not k_4 == k_5:
for k_6 in order:
v_6 = order[k_6]
v_6_t = str(v_6) + str(v_6)
v_6_h = str(v_6) + str(v_6)
if v_6_h == v_5_t and not k_1 == k_6 and not k_2 == k_6 and not k_3 == k_6 and not k_4 == k_6 and not k_5 == k_6:
if v_1_h == v_6_t:
print "%s(%s) %s(%s) %s(%s) %s(%s) %s(%s) %s(%s)" % (v_1, k_1, v_2, k_2, v_3, k_3, v_4, k_4, v_5, k_5, v_6, k_6)
print v_1 + v_2 + v_3 + v_4 + v_5 + v_6