LoginSignup
1

More than 5 years have passed since last update.

Project Euler 45「三角数, 五角数, 六角数」

Posted at

Problem 45 「三角数, 五角数, 六角数」

三角数, 五角数, 六角数は以下のように生成される.

三角数 Tn=n(n+1)/2  1, 3, 6, 10, 15, ...
五角数 Pn=n(3n-1)/2 1, 5, 12, 22, 35, ...
六角数 Hn=n(2n-1)   1, 6, 15, 28, 45, ...

T285 = P165 = H143 = 40755であることが分かる.
次の三角数かつ五角数かつ六角数な数を求めよ.

def hoge():
    n = 143
    while True:
        n += 1
        Hn = n * (2 * n - 1) # 増加幅の大きい六角数を基に
        # 五角数と三角数のチェック
        if (((24 * Hn + 1) ** 0.5 + 1) / 6).is_integer() and \
           (((8 * Hn + 1) ** 0.5 - 1) / 2).is_integer():
            return Hn

print(hoge())

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1