-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygonal_representations.sf
82 lines (64 loc) · 2.67 KB
/
polygonal_representations.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
81
82
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 08 March 2018
# https://github.com/trizen
# Find all the possible polygonal representations P(a,b) for a given number `n`.
# Example:
# 235 = P(5, 25) = P(235, 2) = P(10, 7)
# See also:
# https://oeis.org/A176774
func polygonal_representations (n) {
var divisors = (2*n).divisors
divisors.shift
var representations = []
divisors.each { |d|
var t = (d - 1)
var k = (2*n / d + 2*d - 4)
if (t `divides` k) {
representations << [d, k/t]
}
}
return representations
}
for k in (1 .. 30) {
var n = (2**k + 1)
var P = polygonal_representations(n)
# Display the solutions
say %Q<2^#{k} + 1 = #{P.map{|a| "P(#{a[0]}, #{a[1]})"}.join(' = ')}>
# Verify the solutions
assert(P.all {|a| a[0].polygonal(a[1]) == n })
}
assert_eq(polygonal_representations(165), [[2, 165], [3, 56], [5, 18], [165, 2]])
assert_eq(polygonal_representations(55), [[2, 55], [5, 7], [10, 3], [55, 2]])
assert_eq(polygonal_representations(9994), [[2, 9994], [4, 1667], [9994, 2]])
__END__
2^1 + 1 = P(2, 3) = P(3, 2)
2^2 + 1 = P(2, 5) = P(5, 2)
2^3 + 1 = P(2, 9) = P(3, 4) = P(9, 2)
2^4 + 1 = P(2, 17) = P(17, 2)
2^5 + 1 = P(2, 33) = P(3, 12) = P(33, 2)
2^6 + 1 = P(2, 65) = P(5, 8) = P(65, 2)
2^7 + 1 = P(2, 129) = P(3, 44) = P(129, 2)
2^8 + 1 = P(2, 257) = P(257, 2)
2^9 + 1 = P(2, 513) = P(3, 172) = P(9, 16) = P(513, 2)
2^10 + 1 = P(2, 1025) = P(5, 104) = P(1025, 2)
2^11 + 1 = P(2, 2049) = P(3, 684) = P(2049, 2)
2^12 + 1 = P(2, 4097) = P(17, 32) = P(4097, 2)
2^13 + 1 = P(2, 8193) = P(3, 2732) = P(8193, 2)
2^14 + 1 = P(2, 16385) = P(5, 1640) = P(16385, 2)
2^15 + 1 = P(2, 32769) = P(3, 10924) = P(9, 912) = P(33, 64) = P(32769, 2)
2^16 + 1 = P(2, 65537) = P(65537, 2)
2^17 + 1 = P(2, 131073) = P(3, 43692) = P(131073, 2)
2^18 + 1 = P(2, 262145) = P(5, 26216) = P(65, 128) = P(262145, 2)
2^19 + 1 = P(2, 524289) = P(3, 174764) = P(524289, 2)
2^20 + 1 = P(2, 1048577) = P(17, 7712) = P(1048577, 2)
2^21 + 1 = P(2, 2097153) = P(3, 699052) = P(9, 58256) = P(129, 256) = P(2097153, 2)
2^22 + 1 = P(2, 4194305) = P(5, 419432) = P(4194305, 2)
2^23 + 1 = P(2, 8388609) = P(3, 2796204) = P(8388609, 2)
2^24 + 1 = P(2, 16777217) = P(257, 512) = P(16777217, 2)
2^25 + 1 = P(2, 33554433) = P(3, 11184812) = P(33, 63552) = P(33554433, 2)
2^26 + 1 = P(2, 67108865) = P(5, 6710888) = P(67108865, 2)
2^27 + 1 = P(2, 134217729) = P(3, 44739244) = P(9, 3728272) = P(513, 1024) = P(134217729, 2)
2^28 + 1 = P(2, 268435457) = P(17, 1973792) = P(268435457, 2)
2^29 + 1 = P(2, 536870913) = P(3, 178956972) = P(536870913, 2)
2^30 + 1 = P(2, 1073741825) = P(5, 107374184) = P(65, 516224) = P(1025, 2048) = P(1073741825, 2)