Permutations: Difference between revisions
Content added Content deleted
m (→numbers: added whitespace, changed comments and indentation. -- ~~~~) |
|||
Line 2,608: | Line 2,608: | ||
This version is modeled after the Maxima program (as far as output). |
This version is modeled after the Maxima program (as far as output). |
||
<br><br>It doesn't have the formatting capabilities of REXX version 1, nor can it handle taking '''X''' items taken ''' Y ''' at-a-time. |
<br><br>It doesn't have the formatting capabilities of REXX version 1, nor can it handle taking '''X''' items taken ''' Y ''' at-a-time. |
||
<lang rexx>/*REXX program shows permutations of |
<lang rexx>/*REXX program shows permutations of N number of objects (1,2,3, ...).*/ |
||
parse arg n .; if n=='' then n=3 /*Not specified? |
parse arg n .; if n=='' then n=3 /*Not specified? Assume default.*/ |
||
/*populate the first permutation.*/ |
/*populate the first permutation.*/ |
||
do pop=1 for n; |
do pop=1 for n; @.pop=pop ; end; call tell n |
||
do while nextperm(n,0); call tell n; end |
do while nextperm(n,0); call tell n; end |
||
Line 2,618: | Line 2,618: | ||
nextperm: procedure expose @.; parse arg n,i; nm=n-1 |
nextperm: procedure expose @.; parse arg n,i; nm=n-1 |
||
do k=nm by -1 for nm; |
do k=nm by -1 for nm; kp=k+1 |
||
if @.k<@.kp then do; |
if @.k<@.kp then do; i=k; leave; end |
||
end /*k*/ |
end /*k*/ |
||
do j=i+1 while j<n; parse value @.j @.n with @.n @.j; n=n-1; end |
do j=i+1 while j<n; parse value @.j @.n with @.n @.j; n=n-1; end |
||
if i==0 then return 0 |
if i==0 then return 0 |
||
do j=i+1 while @.j<@.i; end |
do j=i+1 while @.j<@.i; end |
||
parse value @.j @.i with @.i @.j |
parse value @.j @.i with @.i @.j |
||
return 1 |
return 1 |
||
/*──────────────────────────────────TELL subroutine─────────────────────*/ |
/*──────────────────────────────────TELL subroutine─────────────────────*/ |