Skip to content

Instantly share code, notes, and snippets.

@pjc
Created July 2, 2012 00:58
Show Gist options
  • Save pjc/3030291 to your computer and use it in GitHub Desktop.
Save pjc/3030291 to your computer and use it in GitHub Desktop.
Euler Project in Ruby - Problem 9
# Euclid's formula yields Pythagorean triples for integers m and n with m < n:
# a = m**2 - n**2 ; b = 2*m*n ; c = m**2 + n**2
x = 1000
def euclids upto
result = []
(2..upto).each do |m| # Start at 2 as 1 results nothing anyway
(1...m).each do |n| # Euclid's formula only works for m > n
result << [m**2 - n**2, 2*m*n, m**2 + n**2]
end
end
result
end
# Calling Euclids on n/2 suffices as m + n < x
euclids(x/2).each do |triple|
a, b, c = triple[0], triple[1], triple[2]
if a + b + c == x
puts a * b * c
break
end
end
@stevenpetryk
Copy link

stop typing in giant letters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment