sum_of_two_rectangles_solutions.sf 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/ruby
  2. # Author: Trizen
  3. # Date: 18 March 2022
  4. # https://github.com/trizen
  5. # Represent an integer as a sum of two rectangles:
  6. #
  7. # n = a*b + c*d
  8. #
  9. # with a,b,c,d > 1 and a != b and c != d.
  10. func difference_of_two_squares_solutions(n) {
  11. n.divisors.map {|d|
  12. break if (d*d >= n)
  13. var a = d
  14. var b = n/d
  15. (a+b).is_even || next
  16. var x = (a + b)/2
  17. var y = (b - a)/2
  18. [x, y]
  19. }
  20. }
  21. func sum_of_two_rectangles_solutions(n) {
  22. var solutions = []
  23. for x in (1 .. (n>>1)) {
  24. var y = (n - x)
  25. var ab = []
  26. var cd = []
  27. difference_of_two_squares_solutions(x).each_2d {|a,b|
  28. ab << [a+b, a-b] if (a-b > 1)
  29. }
  30. difference_of_two_squares_solutions(y).each_2d {|c,d|
  31. cd << [c+d, c-d] if (c-d > 1)
  32. }
  33. [ab,cd].cartesian {|x,y|
  34. solutions << [x..., y...]
  35. }
  36. }
  37. return solutions.sort
  38. }
  39. var n = 48
  40. sum_of_two_rectangles_solutions(n).each_2d {|a,b,c,d|
  41. say "#{a*b + c*d} = #{a}*#{b} + #{c}*#{d}"
  42. }
  43. __END__
  44. 48 = 4*2 + 10*4
  45. 48 = 4*2 + 20*2
  46. 48 = 5*3 + 11*3
  47. 48 = 6*2 + 18*2
  48. 48 = 6*4 + 6*4
  49. 48 = 6*4 + 12*2
  50. 48 = 7*3 + 9*3
  51. 48 = 8*2 + 8*4
  52. 48 = 8*2 + 16*2
  53. 48 = 10*2 + 14*2
  54. 48 = 12*2 + 6*4
  55. 48 = 12*2 + 12*2