鱼C论坛

 找回密码
 立即注册
查看: 4552|回复: 15

题目44:找出最小的和与差都是五角数的五角数对

[复制链接]
发表于 2017-8-18 23:24:12 | 显示全部楼层
  1. import time
  2. s=time.clock()
  3. from itertools import takewhile
  4. def pentagon(init=None):
  5.     if init is None:
  6.         init=1
  7.     while not ispentagon(init):
  8.         init+=1
  9.     n=((1+24*init)**0.5+1)//6
  10.     while True:
  11.         yield n*(3*n-1)//2
  12.         n+=1

  13. def ispentagon(n):
  14.     flag=False
  15.     if int(((1+24*n)**0.5+1)/6)==((1+24*n)**0.5+1)/6:
  16.         flag=True
  17.     return flag

  18. flag=False
  19. for p1 in pentagon(3):
  20.     if flag==True:
  21.         break
  22.     for p2 in takewhile(lambda x:x<p1,pentagon(1)):
  23.         if ispentagon(p1+p2) and ispentagon(p1-p2):
  24.             print(int(p1-p2))
  25.             flag=True
  26.             break
  27. print('Runing time: {0}s'.format(round(time.clock()-s,3)))
复制代码

5482660
Runing time: 3.316s
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-27 04:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表