Skip to content

Instantly share code, notes, and snippets.

@raywu
Created March 19, 2012 03:42
Show Gist options
  • Save raywu/2093533 to your computer and use it in GitHub Desktop.
Save raywu/2093533 to your computer and use it in GitHub Desktop.
Project Euler problem 9
found = false
(1..1E3).each { |x|
(1..1E3).each { |y|
puts "a = #{x}, b = #{y}, c = #{1000 - x - y}, product = #{x * y * (1000 - x - y)}"
found = true if x ** 2 + y ** 2 == (1000 - x - y) ** 2
break if found
}
break if found
}
# 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.
found = []
(1..1E3).each { |x|
(1..1E3).each { |y|
puts "a = #{x}, b = #{y}, c = #{1000 - x - y}, product = #{x * y * (1000 - x - y)}"
found << [x, y, (1000 - x - y)] if x ** 2 + y ** 2 == (1000 - x - y) ** 2
}
}
p found.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment