-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifference_of_two_rectangles_solutions.sf
80 lines (59 loc) · 1.51 KB
/
difference_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
77
78
79
80
#!/usr/bin/ruby
# Author: Trizen
# Date: 18 March 2022
# https://github.com/trizen
# Represent an integer as a difference of two rectangles:
#
# n = a*b - c*d
#
# with a,b,c,d > 1 and a != b and c != d.
# This algorithm works only for integers that can also be represented as a difference of two squares.
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 difference_of_two_rectangles_solutions(n) {
var solutions = []
difference_of_two_squares_solutions(n).each_2d {|x,y|
var ab = []
var cd = []
difference_of_two_squares_solutions(x**2).each_2d {|a,b|
ab << [a+b, a-b] if (a-b > 1)
}
difference_of_two_squares_solutions(y**2).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 = 420
difference_of_two_rectangles_solutions(n).each_2d {|a,b,c,d|
say "#{a*b - c*d} = #{a}*#{b} - #{c}*#{d}"
}
__END__
420 = 242*2 - 16*4
420 = 242*2 - 32*2
420 = 338*2 - 32*8
420 = 338*2 - 64*4
420 = 338*2 - 128*2
420 = 722*2 - 64*16
420 = 722*2 - 128*8
420 = 722*2 - 256*4
420 = 722*2 - 512*2
420 = 5618*2 - 208*52
420 = 5618*2 - 338*32
420 = 5618*2 - 416*26
420 = 5618*2 - 676*16
420 = 5618*2 - 1352*8
420 = 5618*2 - 2704*4
420 = 5618*2 - 5408*2