Project Euler Problem 9
問題
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
ソース
n = 1000 0.step((n - 3) / 3, 1) do |a| (a + 1).step((n - a - 1) / 2, 1) do |b| c = n - a - b if a ** 2 + b ** 2 == c ** 2 printf("%s => %d\n", [a, b, c].join(", "), (a * b * c)) end end end
解答
200, 375, 425 => 31875000
感想
まず、この問題でへ〜〜〜〜だった。
でもとりあえず、これも簡単!