Ordered partitions: Difference between revisions
m
→{{header|Mathematica}}
(Add Rust implementation) |
|||
Line 1,682:
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
This code works as follows:
'''Permutations''' finds all permutations of the numbers ranging from 1 to n.
'''w''' finds the required partition for an individual permutation.
'''m''' finds partitions for all permutations.
'''Sort''' and '''Union''' eliminate duplicates.
<lang Mathematica>w[partitions_]:=Module[{s={},t=Total@partitions,list=partitions,k}, n=Length[list];▼
<lang Mathematica>▼
▲w[partitions_]:=Module[{s={},t=Total@partitions,list=partitions,k}, n=Length[list];
While[n>0,s=Join[s,{Take[t,(k=First[list])]}];t=Drop[t,k];list=Rest[list];n--]; s]
m[p_]:=(Sort/@#)&/@(w[#,p]&/@Permutations[Range@Total[p]])//Union</lang>▼
▲m[p_]:=(Sort/@#)&/@(w[#,p]&/@Permutations[Range@Total[p]])//Union
'''Usage'''
Grid displays the output in a table.
▲<lang Mathematica>Grid@m[{2, 0, 2}]
Grid@m[{1, 1, 1}]</lang>▼
▲Grid@m[{1, 1, 1}]
[[File:Example.png]]
|