# Euler Problem 61

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)=n^(2)   	  	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.

# Solutions

## Python by Althalus

Runtime between 100 ms and 2 seconds (Undoubtedly due to randomly generating the order to check the lists in (and then not tracking which orders have already been examined)

```import time
from random import randint
start_time = time.time()

polys = [] #0 = tri, 1 = squ 2 = pent 3 = hex 4 = hept 5 = oct
polys.append([i for i in [i*(i+1)/2 for i in range(2,1000)] if i < 10000 and i > 999])
polys.append([i for i in [i**2 for i in range(2,1000)] if i < 10000 and i > 999])
polys.append([i for i in [i*(3*i-1)/2 for i in range(2,1000)] if i < 10000 and i > 999])
polys.append([i for i in [i*(2*i-1) for i in range(2,1000)] if i < 10000 and i > 999])
polys.append([i for i in [i*(5*i-3)/2 for i in range(2,1000)] if i < 10000 and i > 999])
polys.append([i for i in [i*(3*i-2) for i in range(2,1000)] if i < 10000 and i > 999])

#We'll sue an exit statement when we find the solution.
while True:
#Get 0 to 5 in a random order.
order = []
while len(order) < 6:
rand = randint(0,5)
if rand not in order: order.append(rand)
for i in [k for k in polys[order[0]] if k < 10000 and k > 999]:
for j in [k for k in polys[order[1]] if k//100 == i%100 and k < 10000 and k > 999]:
for l in [k for k in polys[order[2]] if k//100 == j%100 and k < 10000 and k > 999]:
for m in [k for k in polys[order[3]] if k//100 == l%100 and k < 10000 and k > 999]:
for n in [k for k in polys[order[4]] if k//100 == m%100 and k < 10000 and k > 999]:
for q in [k for k in polys[order[5]] if k//100 == n%100 and  k < 10000 and k > 999]:
if q%100 == i//100:
print i,j,l,m,n,q
print sum([i,j,l,m,n,q])
print time.time() - start_time
exit()
```