# -*- coding: utf-8 -*- # http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2045 # # 三角数 # y = t(n) # として y が 五角数かつ六角数であるか判定する. # # 五角数の逆関数 # n = (Math.sqrt(24 * y + 1) + 1) / 6.0 # が整数のとき,y は五角数である. # # 六角数の逆関数 # n = (Math.sqrt(8 * y + 1) + 1) / 4.0 # が整数のとき,y は六角数である. # def t(n) n * (n + 1) / 2 end def p?(y) n = (Math.sqrt(24 * y + 1) + 1) / 6.0 n.to_i == n end def h?(y) n = (Math.sqrt(8 * y + 1) + 1) / 4.0 n.to_i == n end c = 0 i = 1 y = nil # 1, 285,の次の三角数かつ五角数かつ六角数な数が出るまで続ける while (c < 3) y = t(i) if (p?(y) && h?(y)) c += 1 end i = i.next end p y