Here is what I’m doing:
(1..999).each do |a|
(1..999).each do |b|
if Math.sqrt(a**2 + b**2) % 1 == 0 && a + b + Math.sqrt(a**2 + b**2) == 1000 && a >= b
puts a * b * Math.sqrt(a**2 + b**2)
end
end
end
What is happening is that a and b are interchangeable in the formulas so there are two matches and thus puts gets outputted twice. To fix this, I added a >= b and now it only gets outputted once. But, if a == bit outputs it twice. I know that a and b will always be different in the example I’m using, but this seems like bad design to me.
Two questions:
-
Is there a better pattern in Ruby for taking an array and comparing it to it self?
-
How can I avoid it outputting twice always. I could set a variable that if changed before the start of the next loop would break out. Is that the proper way to do this?
Edited to use a little better practice (each block with first and second for arrays of arrays)