Created
March 19, 2012 03:42
-
-
Save raywu/2093533 to your computer and use it in GitHub Desktop.
Project Euler problem 9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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