Anonymous user
Permutations/Rank of a permutation: Difference between revisions
m
→{{header|REXX}}: added whitespace, fixed a typo.
m (→{{header|REXX}}: added whitespace, fixed a typo.) |
|||
Line 1,740:
<lang rexx>/*REXX program displays permutations of N number of objects (1, 2, 3, ···). */
parse arg N y seed . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=
if y=='' | y=="," then y=
if datatype(seed,'W') then call random ,,seed /*can make RANDOM numbers repeatable. */
permutes= permSets(N) /*returns N! (number of permutations).*/
Line 1,747:
@.=
do p=0 to permutes-1 /*traipse through each of the permutes.*/
z= permSets(N, p)
say 'for' N "items, permute rank" right(p,w) 'is: ' z
@.p=
end /*p*/
say /* [↓] displays a particular perm rank*/
say ' the permutation rank of' y "is: " @.y /*display a particular
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
permSets: procedure expose @. #; #= 0;
do j=1 for x; @.j= j-1; end /*j*/
_=0; do u=2 for xm; _= _ @.u; end /*u*/
if r==# then return _; if c==_ then return #
do while .permSets(x,0); #= #+1; _= @.1
do v=2 for xm; _= _ @.v; end /*v*/
if r==# then return _; if c==_ then return #
end /*while···*/
return #+1
/*──────────────────────────────────────────────────────────────────────────────────────*/
.permSets: procedure expose @.; parse arg p,q; pm= p-1
if q==0 then return 0
end /*p*/
parse value @.p @.q with @.q @.p
return 1</lang>
|