嵇爾的吐槽

#没事画轮子的嵇尔不定期的(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 1, 3, 6, 10, 15, …
Square 1, 4, 9, 16, 25, …
Pentagonal 1, 5, 12, 22, 35, …
Hexagonal 1, 6, 15, 28, 45, …
Heptagonal 1, 7, 18, 34, 55, …
Octagonal 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 (), square (), and pentagonal (), 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)[2] == '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)[2] == '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)[2] == '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)[2] == '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)[2] == '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)[2] == '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[0]:
        v_1 = order[0][k_1]
        v_1_t = str(v_1)[2] + str(v_1)[3]
        v_1_h = str(v_1)[0] + str(v_1)[1]
        for k_2 in order[1]:
            v_2 = order[1][k_2]
            v_2_t = str(v_2)[2] + str(v_2)[3]
            v_2_h = str(v_2)[0] + str(v_2)[1]
            if v_2_h == v_1_t and not k_1 == k_2:
                for k_3 in order[2]:
                    v_3 = order[2][k_3]
                    v_3_t = str(v_3)[2] + str(v_3)[3]
                    v_3_h = str(v_3)[0] + str(v_3)[1]
                    if v_3_h == v_2_t and not k_1 == k_3 and not k_2 == k_3:
                        for k_4 in order[3]:
                            v_4 = order[3][k_4]
                            v_4_t = str(v_4)[2] + str(v_4)[3]
                            v_4_h = str(v_4)[0] + str(v_4)[1]
                            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[4]:
                                    v_5 = order[4][k_5]
                                    v_5_t = str(v_5)[2] + str(v_5)[3]
                                    v_5_h = str(v_5)[0] + str(v_5)[1]
                                    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[5]:
                                            v_6 = order[5][k_6]
                                            v_6_t = str(v_6)[2] + str(v_6)[3]
                                            v_6_h = str(v_6)[0] + str(v_6)[1]
                                            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
评论共