RosettaCodeData/Task/Combinations/Maxima/combinations.maxima

27 lines
528 B
Plaintext

next_comb(n, p, a) := block(
[a: copylist(a), i: p],
if a[1] + p = n + 1 then return(und),
while a[i] - i >= n - p do i: i - 1,
a[i]: a[i] + 1,
for j from i + 1 thru p do a[j]: a[j - 1] + 1,
a
)$
combinations(n, p) := block(
[a: makelist(i, i, 1, p), v: [ ]],
while a # 'und do (v: endcons(a, v), a: next_comb(n, p, a)),
v
)$
combinations(5, 3);
/* [[1, 2, 3],
[1, 2, 4],
[1, 2, 5],
[1, 3, 4],
[1, 3, 5],
[1, 4, 5],
[2, 3, 4],
[2, 3, 5],
[2, 4, 5],
[3, 4, 5]] */