fork download
  1. SUP = 10000
  2.  
  3. def pythagorean_triples sup = SUP
  4. ary = []
  5. 1.step(sup, 2){|a|
  6. aa = a * a
  7. 1.step(a - 2, 2){|b|
  8. next if a.gcd(b) > 1
  9. x, y = [aa - b * b >> 1, a * b].sort
  10. next if y > sup
  11. xx = yy = 0
  12. ary += (sup/y).times.map{[xx += x, yy += y]}
  13. }
  14. }
  15. ary
  16. end
  17.  
  18. pt = pythagorean_triples.sort
  19. while a = pt.shift
  20. s1, s2 = [], []
  21. pt.each{|c, d|
  22. if c == a[0]
  23. s1 << d
  24. elsif c == a[1]
  25. s2 << d
  26. end
  27. }
  28. (s1 & s2).each{|e| p [*a, e]}
  29. end
  30.  
Time limit exceeded #stdin #stdout 5s 57484KB
stdin
Standard input is empty
stdout
[44, 117, 240]
[85, 132, 720]
[88, 234, 480]