読者です 読者をやめる 読者になる 読者になる

mu chance or much chance ?

日々の戯れ言

Project Euler 9

Project Euler

Problem9を解き直しました.

  • Special Pythagorean triplet

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.

https://projecteuler.net/problem=9

問題は「ピタゴラス数(自然数a, b, c,ただし,a < b < c)の合計値が1000における積abcを求めよ」.

for a in 1..1000 - 2 do
  for b in a + 1..1000 - 1 - a do
    c = 1000 - (a + b)
    next unless a < b && b < c
    p a * b * c if c**2 == a**2 + b**2
  end
end