-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_to_expression_form.sf
150 lines (136 loc) · 3.71 KB
/
number_to_expression_form.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 26 May 2019
# https://github.com/trizen
# Represent a given number as an expression, using only addition and subtraction by 1, and multiplication by powers of 2.
# This is done by recursively using the N +/- 1 prime factorization and picking the shortest representation at each step.
func number2expr() {}
func nminus1_representation(n) is cached {
return "1" if (n == 1)
return "2" if (n == 2)
if (n & (n-1) == 0) {
return "2^#{n.valuation(2)}"
}
'(' + (n-1 -> factor_map{|p,e|
var t = number2expr(p)
e == 1 ? t : "#{t}^#{e}"
}.join(' * ')) + ' + 1)'
}
func nplus1_representation(n) is cached {
return "1" if (n == 1)
return "2" if (n == 2)
if (n & (n-1) == 0) {
return "2^#{n.valuation(2)}"
}
'(' + (n+1 -> factor_map{|p,e|
var t = number2expr(p)
e == 1 ? t : "#{t}^#{e}"
}.join(' * ')) + ' - 1)'
}
func number2expr(n) is cached {
[nminus1_representation(n), nplus1_representation(n)].min_by{.len}
}
for k in (1 .. 100) {
say "#{k} = #{number2expr(k)}"
}
__END__
1 = 1
2 = 2
3 = (2 + 1)
4 = 2^2
5 = (2^2 + 1)
6 = ((2^2 + 1) + 1)
7 = (2^3 - 1)
8 = 2^3
9 = (2^3 + 1)
10 = ((2 + 1)^2 + 1)
11 = (2 * (2^2 + 1) + 1)
12 = ((2 * (2^2 + 1) + 1) + 1)
13 = (2^2 * (2 + 1) + 1)
14 = ((2^2 * (2 + 1) + 1) + 1)
15 = (2^4 - 1)
16 = 2^4
17 = (2^4 + 1)
18 = ((2^4 + 1) + 1)
19 = (2 * (2 + 1)^2 + 1)
20 = ((2 * (2 + 1)^2 + 1) + 1)
21 = (2^2 * (2^2 + 1) + 1)
22 = ((2 + 1) * (2^3 - 1) + 1)
23 = (2^3 * (2 + 1) - 1)
24 = ((2^2 + 1)^2 - 1)
25 = (2^3 * (2 + 1) + 1)
26 = ((2 + 1)^3 - 1)
27 = (2^2 * (2^3 - 1) - 1)
28 = ((2 + 1)^3 + 1)
29 = (2^2 * (2^3 - 1) + 1)
30 = ((2^5 - 1) - 1)
31 = (2^5 - 1)
32 = 2^5
33 = (2^5 + 1)
34 = ((2^2 + 1) * (2^3 - 1) - 1)
35 = (2 * (2^4 + 1) + 1)
36 = ((2^2 + 1) * (2^3 - 1) + 1)
37 = (2^2 * (2 + 1)^2 + 1)
38 = ((2^2 * (2 + 1)^2 + 1) + 1)
39 = (2^3 * (2^2 + 1) - 1)
40 = ((2^3 * (2^2 + 1) + 1) - 1)
41 = (2^3 * (2^2 + 1) + 1)
42 = ((2^3 * (2^2 + 1) + 1) + 1)
43 = (2 * (2 + 1) * (2^3 - 1) + 1)
44 = ((2 + 1)^2 * (2^2 + 1) - 1)
45 = (2 * (2^3 * (2 + 1) - 1) - 1)
46 = ((2^4 * (2 + 1) - 1) - 1)
47 = (2^4 * (2 + 1) - 1)
48 = ((2^3 - 1)^2 - 1)
49 = (2^4 * (2 + 1) + 1)
50 = ((2^3 - 1)^2 + 1)
51 = (2 * (2^2 + 1)^2 + 1)
52 = ((2 + 1) * (2^4 + 1) + 1)
53 = (2 * (2 + 1)^3 - 1)
54 = ((2 * (2 + 1)^3 - 1) + 1)
55 = (2 * (2 + 1)^3 + 1)
56 = ((2 + 1) * (2 * (2 + 1)^2 + 1) - 1)
57 = (2^3 * (2^3 - 1) + 1)
58 = ((2 + 1) * (2 * (2 + 1)^2 + 1) + 1)
59 = (2 * (2^2 * (2^3 - 1) + 1) + 1)
60 = ((2 * (2^5 - 1) - 1) - 1)
61 = (2 * (2^5 - 1) - 1)
62 = ((2 * (2^5 - 1) - 1) + 1)
63 = (2^6 - 1)
64 = 2^6
65 = (2^6 + 1)
66 = ((2^2 * (2^4 + 1) - 1) - 1)
67 = (2^2 * (2^4 + 1) - 1)
68 = ((2^2 * (2^4 + 1) - 1) + 1)
69 = (2^2 * (2^4 + 1) + 1)
70 = ((2^3 * (2 + 1)^2 - 1) - 1)
71 = (2^3 * (2 + 1)^2 - 1)
72 = ((2^3 * (2 + 1)^2 - 1) + 1)
73 = (2^3 * (2 + 1)^2 + 1)
74 = ((2^3 * (2 + 1)^2 + 1) + 1)
75 = (2 * (2^2 * (2 + 1)^2 + 1) + 1)
76 = ((2 + 1) * (2^2 + 1)^2 + 1)
77 = (2^2 * (2 * (2 + 1)^2 + 1) + 1)
78 = ((2^4 * (2^2 + 1) - 1) - 1)
79 = (2^4 * (2^2 + 1) - 1)
80 = ((2 + 1)^4 - 1)
81 = (2^4 * (2^2 + 1) + 1)
82 = ((2 + 1)^4 + 1)
83 = (2 * (2^3 * (2^2 + 1) + 1) + 1)
84 = ((2^2 + 1) * (2^4 + 1) - 1)
85 = (2^2 * (2 + 1) * (2^3 - 1) + 1)
86 = ((2^2 + 1) * (2^4 + 1) + 1)
87 = (2^3 * (2 * (2^2 + 1) + 1) - 1)
88 = ((2 + 1) * (2^2 * (2^3 - 1) + 1) + 1)
89 = (2^3 * (2 * (2^2 + 1) + 1) + 1)
90 = ((2^3 * (2 * (2^2 + 1) + 1) + 1) + 1)
91 = (2 * (2 + 1)^2 * (2^2 + 1) + 1)
92 = ((2 + 1) * (2^5 - 1) - 1)
93 = (2 * (2^4 * (2 + 1) - 1) - 1)
94 = ((2 + 1) * (2^5 - 1) + 1)
95 = (2^5 * (2 + 1) - 1)
96 = ((2^5 * (2 + 1) + 1) - 1)
97 = (2^5 * (2 + 1) + 1)
98 = ((2^5 * (2 + 1) + 1) + 1)
99 = (2 * (2^3 - 1)^2 + 1)
100 = ((2^2 * (2^2 + 1)^2 + 1) - 1)