题目:
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
Triangle | Tn=n(n+1)/2 | 1, 3, 6, 10, 15, ... | ||
Pentagonal | Pn=n(3n![]() | 1, 5, 12, 22, 35, ... | ||
Hexagonal | Hn=n(2n![]() | 1, 6, 15, 28, 45, ... |
It can be verified that T285 = P165 = H143 = 40755.
Find the next triangle number that is also pentagonal and hexagonal.
代码为:__author__ = 'chenqinghe'
import math,time
t=time.time()
def isTriangular(n):
return int(math.sqrt(n))**2+int(math.sqrt(n))==n
def ispentagonal(n):
return int((math.sqrt(24*n+1)+1)/6.0)==(math.sqrt(24*n+1)+1)/6.0
n=144
step=lambda n:4*n-3
startnum=40755
while 1:
startnum+=step(n)
if ispentagonal(startnum) and ispentagonal(startnum):
print startnum
break
n+=1
print time.time()-t
运行结果为:
1533776805
0.0469999313354