Skip to content

Instantly share code, notes, and snippets.

@aptinio
Created November 4, 2009 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aptinio/225967 to your computer and use it in GitHub Desktop.
Save aptinio/225967 to your computer and use it in GitHub Desktop.
Project Euler Problem 9
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