-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum_of_two_rectangles_solutions.sf
76 lines (55 loc) · 1.24 KB
/
sum_of_two_rectangles_solutions.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/ruby
# Author: Trizen
# Date: 18 March 2022
# https://github.com/trizen
# Represent an integer as a sum of two rectangles:
#
# n = a*b + c*d
#
# with a,b,c,d > 1 and a != b and c != d.
func difference_of_two_squares_solutions(n) {
n.divisors.map {|d|
break if (d*d >= n)
var a = d
var b = n/d
(a+b).is_even || next
var x = (a + b)/2
var y = (b - a)/2
[x, y]
}
}
func sum_of_two_rectangles_solutions(n) {
var solutions = []
for x in (1 .. (n>>1)) {
var y = (n - x)
var ab = []
var cd = []
difference_of_two_squares_solutions(x).each_2d {|a,b|
ab << [a+b, a-b] if (a-b > 1)
}
difference_of_two_squares_solutions(y).each_2d {|c,d|
cd << [c+d, c-d] if (c-d > 1)
}
[ab,cd].cartesian {|x,y|
solutions << [x..., y...]
}
}
return solutions.sort
}
var n = 48
sum_of_two_rectangles_solutions(n).each_2d {|a,b,c,d|
say "#{a*b + c*d} = #{a}*#{b} + #{c}*#{d}"
}
__END__
48 = 4*2 + 10*4
48 = 4*2 + 20*2
48 = 5*3 + 11*3
48 = 6*2 + 18*2
48 = 6*4 + 6*4
48 = 6*4 + 12*2
48 = 7*3 + 9*3
48 = 8*2 + 8*4
48 = 8*2 + 16*2
48 = 10*2 + 14*2
48 = 12*2 + 6*4
48 = 12*2 + 12*2