嵇爾的吐槽

#没事画轮子的嵇尔不定期的(W)碎(E)碎(B)念(B)和(L)吐(O)槽(G)

Problem 45 Triangular, pentagonal, and hexagonal 2016-11-04 09:39:00

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:

评论共