Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 20 lines (17 sloc) 0.476 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Problem 9:
# Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000

sum = 1000
catch(:done) do
  (3..sum).each do |three|
    (2..three).each do |two|
      (1..two).each do |one|
        next unless (one + two + three == sum)
        next unless (one**2 + two**2 == three**2)
        puts "Pythagorean triplet = {#{one}, #{two}, #{three}}, product = #{one*two*three}"
        throw :done
      end
    end
  end
end

# Answer: 31875000
# Time: 0m12.684s
Something went wrong with that request. Please try again.