Anonymous user
Permutations/Rank of a permutation: Difference between revisions
m
→{{header|REXX}}: changed/added comments and whitespace, implemented some semantic changes.
m (added whitespace before the TOC (table of contents), added other whitespace to the task's preamble.) |
m (→{{header|REXX}}: changed/added comments and whitespace, implemented some semantic changes.) |
||
Line 965:
=={{header|REXX}}==
The '''permsets''' subroutine (actually, a function) is a modified version of
Since this REXX program generates permutations without recursive calls,
testing for the limit for a ''stack overflow'' wouldn't be possible using this REXX program.
<lang rexx>/*REXX program
parse arg N seed .
permutes=permSets(N) /*returns N! (number of permutations).*/
w=length(permutes)
z=permsets(N, what) /*get the "what" permuation. */▼
say N 'items, permute' right(what,w) "=" z ' rank=' permsets(N,,z)▼
end /*what*/▼
do what=0 to permutes-1 /*traipse through each of the permutes.*/
▲say; if seed\=='' then call random ,,seed /*seed ≡ repeatability.*/
▲ end /*what*/
say
N=12
do 4; ?=random(0, N**4) /*REXX has a 100k RANDOM range. */
say N 'items, permute' right(?,6) " is "
end /*rand*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
do j=1 for x; @.j=j-1;
if r==# then return _; if c==_ then return #▼
do while .
do v=2 for xm; _=_ @.v; end /*v*/
▲ if r==# then return _; if c==_ then return #
if r==# then return _; if c==_ then return #
end /*while···*/
return #+1
/*──────────────────────────────────────────────────────────────────────────────────────*/
do k=pm by -1 for pm; kp=k+1; if @.k<@.kp then do; q=k; leave; end
end /*k*/
do j=q+1 while j<p; parse value @.j @.p with @.p @.j; p=p-1
▲.permsets: procedure expose @.; parse arg p,q; pm=p-1
do p=q+1 while @.p<@.q; end /*p*/
return 1</lang>
<pre>
4 items, permute 0 = 0 1 2 3 rank= 0
4 items, permute 1 = 0 1 3 2 rank= 1
Line 1,030 ⟶ 1,038:
4 items, permute 23 = 3 2 1 0 rank= 23
12 items, permute
12 items, permute
12 items, permute
12 items, permute
</pre>
|