Project Euler Problem 45
問題
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle T(n)=n(n+1)/2 1, 3, 6, 10, 15, ... Pentagonal P(n)=n(3n−1)/2 1, 5, 12, 22, 35, ... Hexagonal H(n)=n(2n−1) 1, 6, 15, 28, 45, ... It can be verified that T(285) = P(165) = H(143) = 40755. Find the next triangle number that is also pentagonal and hexagonal.
ソース
t, ti = 1, 1 e, ei = 1, 1 h, hi = 1, 1 while true min = [t, e, h].min t, ti = ti * (ti + 1) / 2, ti +1 if min == t e, ei = ei * (ei * 3 - 1) / 2, ei + 1 if min == e h, hi = hi * (hi * 2 - 1), hi + 1 if min == h break if t == e && e == h && t > 40755 end puts t
解答
1533776805
感想
かなり大きい数字だけど、一瞬で答えが出ます。
結構これ以上無いというアルゴリズムだと思いますw