1
2
3
4
5 (define-module (DS math partitions))
6 (define-public DS-math-partitions-VERSION "2006 June 7")
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
57
58
59
60
61
62
63
64
65 (define-public (partitions n)
66 (if (zero? n)
67 (list '())
68 (let ((p (list n 0))
69 (l '()))
70 (set! n 0)
71 (while (not (negative? (car p)))
72
73 (while (> n (car p))
74
75 (set! n (- n (car p)))
76 (set! p (cons (car p) p)))
77 (if (positive? n)
78 (begin
79 (set! p (cons n p))
80 (set! n 0)))
81
82 (set! l (append! l (list (cdr (reverse p)))))
83 (while (= 1 (car p))
84
85 (set! p (cdr p))
86 (set! n (1+ n)))
87
88 (set! n (1+ n))
89 (set-car! p (1- (car p))))
90 l)))
92
93
94
95
96 (define-public (partitions-C n)
97 (if (zero? n)
98 1
99 (integer-expt 2 (1- n))))
101
102
103
104
105 (define-public (partitions-P n)
106 (if (zero? n)
107 1
108 (let ((p (list n 0))
109 (nb 0))
110 (set! n 0)
111 (while (not (negative? (car p)))
112
113 (while (> n (car p))
114
115 (set! n (- n (car p)))
116 (set! p (cons (car p) p)))
117 (if (positive? n)
118 (begin
119 (set! p (cons n p))
120 (set! n 0)))
121
122 (set! nb (1+ nb))
123 (while (= 1 (car p))
124
125 (set! p (cdr p))
126 (set! n (1+ n)))
127
128 (set! n (1+ n))
129 (set-car! p (1- (car p))))
130 nb)))
132
133
134
135
136
137 (define-public (partitions-Pk n k)
138 (if (zero? n)
139 (if (zero? k)
140 1
141 0)
142 (let ((p (list n 0))
143 (len 1)
144 (nb 0))
145 (set! n 0)
146 (while (not (negative? (car p)))
147
148 (while (> n (car p))
149
150 (set! n (- n (car p)))
151 (set! p (cons (car p) p))
152 (set! len (1+ len)))
153 (if (positive? n)
154 (begin
155 (set! p (cons n p))
156 (set! n 0)
157 (set! len (1+ len))))
158
159 (if (= len k)
160 (set! nb (1+ nb)))
161 (while (= 1 (car p))
162
163 (set! p (cdr p))
164 (set! n (1+ n))
165 (set! len (1- len)))
166
167 (set! n (1+ n))
168 (set-car! p (1- (car p))))
169 nb)))
171
172
173
174
175
176 (define-public (partitions-P-even n)
177 (if (or (odd? n) (zero? n))
178 (if (zero? n)
179 1
180 0)
181 (let ((p (list n 0))
182 (nb 0))
183 (set! n 0)
184 (while (not (negative? (car p)))
185
186 (while (> n (car p))
187
188 (set! n (- n (car p)))
189 (set! p (cons (car p) p)))
190 (if (positive? n)
191 (begin
192 (set! p (cons n p))
193 (set! n 0)))
194
195 (set! nb (1+ nb))
196 (while (= 2 (car p))
197
198 (set! p (cdr p))
199 (set! n (+ n 2)))
200
201 (set! n (+ n 2))
202 (set-car! p (- (car p) 2)))
203 nb)))
205
206
207
208
209
210
211 (define-public (partitions-P-odd n)
212 (if (zero? n)
213 1
214 (let ((p (list (if (odd? n)
215 n
216 (1- n))
217 0))
218 (nb 0))
219 (set! n (if (odd? n)
220 0
221 1))
222 (while (not (negative? (car p)))
223
224 (while (> n (car p))
225
226 (set! n (- n (car p)))
227 (set! p (cons (car p) p)))
228 (if (odd? n)
229
230 (set! p (cons n p))
231 (if (positive? n)
232
233 (set! p (cons 1 (cons (1- n) p)))))
234 (set! n 0)
235
236 (set! nb (1+ nb))
237 (while (= 1 (car p))
238
239 (set! p (cdr p))
240 (set! n (1+ n)))
241
242 (set! n (+ n 2))
243 (set-car! p (- (car p) 2)))
244 nb)))