Permutations/Derangements: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the outputs.)
Line 2,678: Line 2,678:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm generates all permutations of N derangements & subfactorial #*/
<lang rexx>/*REXX program generates all permutations of N derangements and subfactorial # */
numeric digits 1000 /*be able to handle big subfacts.*/
numeric digits 1000 /*be able to handle large subfactorials*/
parse arg N .; if N=='' then N=4 /*Not specified? Assume default*/
parse arg N .; if N=='' | N=="," then N=4 /*Not specified? Then use the default.*/
d=derangementsSet(N) /*go & build the derangements set*/
d= derangeSet(N) /*go and build the derangements set. */
say d 'derangements for' N "items are:"
say d 'derangements for' N "items are:"
say
say
do i=1 for d /*show derangements for N items.*/
do i=1 for d /*display the derangements for N items.*/
say right('derangement',22) right(i,length(d)) '───►' $.i
say right('derangement', 22) right(i, length(d) ) '───►' $.i
end /*i*/
end /*i*/
say /* [↓] count and calculate !L. */
say /* [↓] count and calculate subfact !L.*/
do L=0 to 9; d=derangementsSet(L)
do L=0 to 2; d= derangeSet(L)
say L 'items: derangement count='right(d,6)", !"L'='right(!s(L),6)
say L 'items: derangement count='right(d, 6)", !"L'='right( !s(L), 6)
end /*L*/
end /*L*/
say
say
say right('!20=' , 40) !s( 20)
say right('!20=' , 22) !s( 20)
say right('!100=', 40) !s(100)
say right('!200=', 22) !s(200)
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────!S subroutine───────────────────────*/
!s: _=1; do j=1 for arg(1);if j//2 then _=-1+j*_;else _=1+j*_;end;return _
!s: _=1; do j=1 for arg(1); if j//2 then _= j*_ - 1; else _=j*_ + 1
end /*j*/; return _
/*──────────────────────────────────DERANGEMENTSSET subroutine──────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
derangementsSet: procedure expose $.; parse arg x; $.=; #=0; p=x-1
derangeSet: procedure expose $.; parse arg x; $.=; #=0; p=x-1
if x==0 then return 1; if x==1 then return 0
/*populate the first derangement.*/
if x==0 then return 1; if x==1 then return 0
@.1=2; @.2=1; do i=3 to x; @.i=i; end
@.1=2; @.2=1 /*populate 1st derangement.*/
parse value @.p @.x with @.x @.p; call .buildD x /*swap & build.*/
do i=3 to x; @.i=i; end /*i*/ /* " the rest of 'em.*/
/*build others.*/
parse value @.p @.x with @.x @.p; call .buildD x /*swap & build.*/
do while .nextD(x,0); call .buildD x; end
/*build others.*/
do while .nextD(x, 0); call .buildD x; end; return #
return #
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────.BUILDD subroutine──────────────────*/
.buildD: do j=1 for arg(1); if @.j==j then return; end
.buildD: do j=1 for arg(1); if @.j==j then return; end
#=#+1; do j=1 for arg(1); $.#=$.# @.j; end; return
#=#+1; do j=1 for arg(1); $.#= $.# @.j; end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────.NEXTD subroutine───────────────────*/
.nextD: procedure expose @.; parse arg n,i; nm=n-1
.nextD: procedure expose @.; parse arg n,i


do k=nm by -1 for nm; kp=k+1; if @.k<@.kp then do; i=k; leave; end
do k=n-1 by -1 for n-1; kp=k+1; 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 /*j*/

if i==0 then return 0
if i==0 then return 0
do j=i+1 while @.j<@.i; end
do m=i+1 while @.m<@.i; end /*m*/ /* [↓] swap two values. */
parse value @.j @.i with @.i @.j
parse value @.m @.i with @.i @.m; return 1</lang>
{{out|output}}
return 1</lang>
{{out}}
<pre>
<pre>
9 derangements for 4 items are:
9 derangements for 4 items are:
Line 2,738: Line 2,737:
1 items: derangement count= 0, !1= 0
1 items: derangement count= 0, !1= 0
2 items: derangement count= 1, !2= 1
2 items: derangement count= 1, !2= 1
3 items: derangement count= 2, !3= 2
4 items: derangement count= 9, !4= 9
5 items: derangement count= 44, !5= 44
6 items: derangement count= 265, !6= 265
7 items: derangement count= 1854, !7= 1854
8 items: derangement count= 14833, !8= 14833
9 items: derangement count=133496, !9=133496


!20= 895014631192902121
!20= 895014631192902121
!200= 290131015521620609254546237518688936375622413566095185632876940298382875066633305125595907908697818551860745708196640009079772455670451355426573609799907339222509103785567575227183775791345718826220455840965346196540544976439608810006794385963854831693077054723298130736781093200499800934036993104223443563872463385599425635345341317933466521378117877578807421014599223577201
!200= 290131015521620609254546237518688936375622413566095185632876940298382875066633305125595907908697818551860745708196640009079772455670451355426573609799907339222509103785567575227183775791345718826220455840965346196540544976439608810006794385963854831693077054723298130736781093200499800934036993104223443563872463385599425635345341317933466521378117877578807421014599223577201
</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==