Problem 45 Triangular, pentagonal, and hexagonal

2016-11-04 09:39:00   技术   python projecteulor

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)/2 1, 5, 12, 22, 35, …

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

import math


def T(n):
    return (math.sqrt(8 * n + 1) - 1) % 2 == 0


def P(n):
    return (math.sqrt(24 * n + 1) + 1) % 6 == 0


def H(n):
    return (math.sqrt(8 * n + 1) + 1) % 4 == 0


i = 144
while True:
    h = i * (2 * i - 1)
    if T(h) and P(h):
        print h
        break
    i += 1

做完44,这题一点儿难度都木有:smile:

评论已关闭。
评论共