关闭

欧拉计划 61 题

标签: 欧拉计划 61
378人阅读 评论(0) 收藏 举报
分类:

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.

  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.



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




0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:317275次
    • 积分:4663
    • 等级:
    • 排名:第6211名
    • 原创:133篇
    • 转载:74篇
    • 译文:0篇
    • 评论:319条
    最新评论