Jump to content

Permutations: Difference between revisions

adding maxima, trnalation of Ada
(→‎{{header|Qi}}: Forgot insert function)
(adding maxima, trnalation of Ada)
Line 1,219:
4, 1}, {2, 4, 1, 3}, {2, 4, 3, 1}, {3, 1, 2, 4}, {3, 1, 4, 2}, {3, 2, 1, 4}, {3, 2, 4, 1}, {3, 4, 1, 2}, {3, 4, 2, 1}, {4, 1, 2,
3}, {4, 1, 3, 2}, {4, 2, 1, 3}, {4, 2, 3, 1}, {4, 3, 1, 2}, {4, 3, 2, 1}}</pre>
 
=={{header|Maxima}}==
<lang maxima>next_permutation(v) := block([n, i, j, k],
n: length(v), i: 0,
for k: n - 1 thru 1 step -1 do (if v[k] < v[k+1] then (i: k, return())),
j: i + 1, k: n,
while j < k do (t: v[j], v[j]: v[k], v[k]: t, j: j + 1, k: k - 1),
if i = 0 then return(false),
j: i + 1,
while v[j] < v[i] do j: j + 1,
t: v[j], v[j]: v[i], v[i]: t,
true
)$
 
print_perm(n) := block([ ],
v: makelist(i, i, 1, n),
disp(v),
while next_permutation(v) do disp(v)
)$
 
print_perm(3);
/* [1,2,3]
[1,3,2]
[2,1,3]
[2,3,1]
[3,1,2]
[3,2,1] */</lang>
 
=={{header|OCaml}}==
506

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.