Created
November 4, 2009 11:07
-
-
Save aptinio/225967 to your computer and use it in GitHub Desktop.
Project Euler Problem 9
This file contains 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
def pythagorean_triple(m, n) | |
m, n = n, m if m > n | |
[n**2 - m**2, 2*m*n, n**2 + m**2] | |
end | |
infinity = 1.0/0.0 | |
catch :answer_found do | |
(1..infinity).each do |m| | |
(m+1..infinity).each do |n| | |
pt = pythagorean_triple(m, n) | |
sum = pt.reduce(:+) | |
if sum == 1000 | |
a, b, c = pt | |
puts "#{a} + #{b} + #{c} = 1000" | |
puts "#{a} * #{b} * #{c} = #{pt.reduce :*}" | |
throw :answer_found | |
elsif sum > 1000 | |
break | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment