Overview

 

Question

A Pythagorean triplet is a set of three natural numbers, a b c, for which,

a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

click for answer

31875000

Solutions

 

Ruby

 pythagorean.rb https://raw.github.com/addamh/euler/master/009/pythagorean.rb download
#!/usr/bin/env ruby

for a in (1..300)
  for b in (1..400)
    for c in (1..500)
      if a*a + b*b == c*c
        if a+b+c == 1000
          puts a*b*c
          exit
        end
      end
    end
  end
end
$ time ruby pythagorean.rb
real	0m16.831s
user	0m16.791s
sys	0m0.030s