Anonymous user
Permutations/Rank of a permutation: Difference between revisions
Permutations/Rank of a permutation (view source)
Revision as of 23:12, 21 October 2017
, 6 years ago→{{header|REXX}}: added/changed comments and whitespace, used a template for output, changed wording in the REXX section header, changed program to also show a particular permutation rank.
m (→{{header|REXX}}: added/changed comments and whitespace, used a template for output, changed wording in the REXX section header, changed program to also show a particular permutation rank.) |
|||
Line 1,448:
=={{header|REXX}}==
The '''permsets''' subroutine (actually, a function) is a modified version of a REXX subroutine used elsewhere in Rosetta
This modified version starts the permute numbers with '''0''' (zero) instead of '''1'''.
Since this REXX program generates permutations without recursive calls,
testing for the limit for a ''stack overflow'' wouldn't be
<lang rexx>/*REXX program displays permutations of N number of objects (1, 2, 3, ···). */
parse arg N y seed .
if N=='' | N=="," then N=
if y=='' | y=="," then y=17 /* " " " " " " */
if datatype(seed,'W') then call random ,,seed /*can make RANDOM numbers repeatable. */
permutes= permSets(N)
w= length(permutes)
@.=
do
z=permSets(N,
say 'for' N
@.p=z /*define a rank permutation in @ array.*/
end /*
say /* [↓] displays a particular perm rank*/
say ' the permutation rank of' y "is: " @.y /*display a particular permuation rank.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
permSets: procedure expose @. #; #=0; parse arg x,r,c;
do j=1 for x; @.j=j-1; end /*j*/
do v=2 for xm; _=_ @.v; end /*v*/
end
/*──────────────────────────────────────────────────────────────────────────────────────*/
.permSets: procedure expose @.; parse arg p,q; pm=p-1
Line 1,488 ⟶ 1,485:
end /*k*/
do j=q+1 while j<p;
end /*j*/
if q==0 then return 0
Line 1,494 ⟶ 1,491:
parse value @.p @.q with @.q @.p
return 1</lang>
<pre>
for 4 items, permute rank 0
for 4 items, permute rank 1
for 4 items, permute rank 2
for 4 items, permute rank 3
for 4 items, permute rank 4
for 4 items, permute rank 5
for 4 items, permute rank 6
for 4 items, permute rank 7
for 4 items, permute rank 8
for 4 items, permute rank 9
for 4 items, permute rank 10
for 4 items, permute rank 11
for 4 items, permute rank 12
for 4 items, permute rank 13
for 4 items, permute rank 14
for 4 items, permute rank 15
for 4 items, permute rank 16
for 4 items, permute rank 17
for 4 items, permute rank 18
for 4 items, permute rank 19
for 4 items, permute rank 20
for 4 items, permute rank 21
for 4 items, permute rank 22
for 4 items, permute rank 23
</pre>
|