[Project Euler]Problem 45

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(3n1)/2
1, 5, 12, 22, 35, ...

Hexagonal
Hn=n(2n1)
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.

 

找出下一个数既是三角数又是五角数,而且还是六角数。

这道题有了上一题的经验就解的比较容易了

t = [i*(i+1)//2 for i in range(286, 1000001)]

p = [i*(3*i-1)//2 for i in range(166, 1000001)]



st = set(t)

sp = set(p)



n = 144

while True:

    h = n*(2*n - 1)

    if h in st and h in sp:

        print(h)

        break

    else:

        n += 1

 

速度很快,基本0.s就出来。

你可能感兴趣的:(project)