Permutations: Difference between revisions

m
→‎using numbers: added comments and whitespace, changed indentations.
m (→‎{{header|Sidef}}: be consistent)
m (→‎using numbers: added comments and whitespace, changed indentations.)
Line 3,871:
===using numbers===
This version is modeled after the '''Maxima''' program   (as far as output).
 
<br><br>It doesn't have the formatting capabilities of the REXX version 1, &nbsp; nor can it handle taking &nbsp; '''X''' &nbsp; items taken &nbsp; '''Y''' &nbsp; at-a-time.
<lang rexx>/*REXX program showsdisplays permutations of N number of objects (1, 2, 3, ... ···). */
parse arg n .; if n=='' then n=3 /*Not specified? Then use the default.*/
parse arg n .; if n=='' | n=="," then n=3 /*Not specified? Then /*populateuse the first permutationdefault. */
do pop=1 for n; @.pop=pop ; end; call tell n /* [↓] populate the first permutation.*/
do pop=1 while nextPerm(for n,0); call tell n @.pop=pop ; end /*pop */; call tell n
do while nPerm(n, 0); call tell n; end /*while*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
nextPermnPerm: procedure expose @.; parse arg n,i; nm=n-1
do k=nm by -1 for nm; kp=k+1; if @.k<@.kp then do; i=k; leave; end; end /*k*/
 
do j=i+1 while doj<n; k=nm parse byvalue -1 @.j for nm;@.n kp=k+1;with @.n if @.k<@.kpj; then do; in=kn-1; end leave; end/*j*/
if i==0 then endreturn /*k*/0
do m=i+1 while @.m<@.i; end /*m*/
 
do j=i+1 while j<n; parse value @.jm @.n i with @.ni @.j; n=n-1m
return end /*j*/1
 
if i==0 then return 0
 
do m=i+1 while @.m<@.i
end /*m*/
 
parse value @.m @.i with @.i @.m
return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: procedure expose @.; _=; do j=1 for arg(1); _=_ @.j; end; say _; return</lang>
'''output''' &nbsp; when using the default input:
<pre>
1 2 3