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(3n1)/2 | 1, 5, 12, 22, 35, ... | ||
Hexagonal | P6,n=n(2n1) | 1, 6, 15, 28, 45, ... | ||
Heptagonal | P7,n=n(5n3)/2 | 1, 7, 18, 34, 55, ... | ||
Octagonal | P8,n=n(3n2) | 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.
import itertools
def triangle(u):
for i in xrange(1,200):
u.append(i*(i+1)/2)
u=[]
triangle(u)
def square(u):
for i in xrange(1,200):
u.append(i**2)
u1 =[]
square(u1)
def pentagonal(u):
for i in xrange(1,200):
u.append(i*(3*i-1)/2)
u2 =[]
pentagonal(u2)
def hexagonal(u):
for i in xrange(1,200):
u.append(i*(2*i-1))
u3 = []
hexagonal(u3)
def heptagonal(u):
for i in xrange(1,200):
u.append(i*(5*i-3)/2)
u4 = []
heptagonal(u4)
def octagonal(u):
for i in xrange(1,200):
u.append(i*(3*i-2))
u5 = []
octagonal(u5)
def finde4(u):
d=[]
for i in xrange(1,199):
if u[i]<10000 and u[i]>999:
d.append(u[i])
return d
a1 = finde4(u)
a2 = finde4(u1)
a3 = finde4(u2)
a4 = finde4(u3)
a5 = finde4(u4)
a6 = finde4(u5)
pList = [a1,a2,a3,a4,a5,a6]
for p in itertools.permutations(range(5,-1,-1), 6):
for t1 in pList[p[0]]:
for t2 in pList[p[1]]:
if t2/100 != t1%100:
continue
for t3 in pList[p[2]]:
if t3/100 != t2%100:
continue
for t4 in pList[p[3]]:
if t4/100 != t3%100:
continue
for t5 in pList[p[4]]:
if t5/100 != t4%100:
continue
for t6 in pList[p[5]]:
if t6/100 == t5%100 and t6%100 == t1/100:
print t1,t2,t3,t4,t5,t6
else:
continue