Skip to content

Instantly share code, notes, and snippets.

@aptinio

aptinio/euler009.rb

Created Nov 4, 2009
Embed
What would you like to do?
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
You can’t perform that action at this time.