Project Euler

Problem #39

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

Ruby: Running time = 0.471s
def p39
  sols=Hash.new(0)
  (1...500).each do |a|
    (1...a).each do |b|
      csquared=a*a+b*b
      c=Math.sqrt(csquared).to_i
      s=a+b+c
      sols[s]+=1 if(s <= 1000 and c*c==csquared)
    end
  end
  puts sols.to_a.sort{|u,v|u.last<=>v.last}.last.first
end