Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created May 27, 2011 06:33
Show Gist options
  • Save johana-star/994758 to your computer and use it in GitHub Desktop.
Save johana-star/994758 to your computer and use it in GitHub Desktop.
Euler Project Problem #009
# Solution to Project Euler's ninth problem
# http://projecteuler.net/index.php?section=problems&id=9
# Find the product of the Pythagorean triplets whose sum equals 1000.
def find_Pythagorean_triplet(number)
squares = (1..number).to_a.reverse
squares = squares.each_index {|s| squares[s] = squares[s]**2}
p squares
squares.each do |c|
squares.each do |b|
squares.each do |a|
if 0 == c - b - a then
if 1000 == Math.sqrt(a) + Math.sqrt(b) + Math.sqrt(c) then
product = Math.sqrt(a)*Math.sqrt(b)*Math.sqrt(c)
puts product
return product
end
end
end
end
end
end
find_Pythagorean_triplet(500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment