fork download
  1. include Math
  2.  
  3. def main
  4. puts "6 digits"
  5. check_range((100..999), 1000)
  6.  
  7. puts "10 digits"
  8. check_range((10000..99999), 100000)
  9. end
  10.  
  11. def check_range(r, w)
  12. s = 0
  13. r.each do |x|
  14. org = get_org(x, w)
  15. if org != 0
  16. puts "matched: #{org}"
  17. s = s + org
  18. end
  19. end
  20. puts "sum: #{s}"
  21. end
  22.  
  23. # check x^2+y^2==xxyy and returns original number or 0
  24. def get_org(x, w)
  25. a = x * (w - x)
  26. y = sqrt(a).to_i + 1
  27. if a == y * (y - 1)
  28. return x * w + y
  29. else
  30. return 0
  31. end
  32. end
  33.  
  34. main
  35.  
Success #stdin #stdout 0.21s 7412KB
stdin
Standard input is empty
stdout
6 digits
matched: 990100
sum: 990100
10 digits
matched: 1765038125
matched: 2584043776
matched: 7416043776
matched: 8235038125
matched: 9901009901
sum: 29901173703