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 |
<lang rexx>/*REXX program generates all permutations of N derangements and subfactorial # */ |
||
numeric digits 1000 /*be able to handle |
numeric digits 1000 /*be able to handle large subfactorials*/ |
||
parse arg N .; if N=='' |
parse arg N .; if N=='' | N=="," then N=4 /*Not specified? Then use the default.*/ |
||
d= |
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 /* |
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 |
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=' , |
say right('!20=' , 22) !s( 20) |
||
say right('! |
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 _= |
!s: _=1; do j=1 for arg(1); if j//2 then _= j*_ - 1; else _=j*_ + 1 |
||
end /*j*/; return _ |
|||
/*──────────────────────────────────DERANGEMENTSSET subroutine──────────*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
derangeSet: procedure expose $.; parse arg x; $.=; #=0; p=x-1 |
|||
if x==0 then return 1; if x==1 then return 0 |
|||
if x==0 then return 1; if x==1 then return 0 |
|||
@.1=2; @.2=1 |
@.1=2; @.2=1 /*populate 1st derangement.*/ |
||
do i=3 to x; @.i=i; end /*i*/ /* " the rest of 'em.*/ |
|||
parse value @.p @.x with @.x @.p; call .buildD x /*swap & build.*/ |
|||
/*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; |
#=#+1; do j=1 for arg(1); $.#= $.# @.j; end; return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────.NEXTD subroutine───────────────────*/ |
|||
.nextD: procedure expose @.; |
.nextD: procedure expose @.; parse arg n,i |
||
do k= |
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 |
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 m=i+1 while @.m<@.i; end /*m*/ /* [↓] swap two values. */ |
|||
parse value @. |
parse value @.m @.i with @.i @.m; return 1</lang> |
||
⚫ | |||
return 1</lang> |
|||
⚫ | |||
<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 |
|||
!200= 290131015521620609254546237518688936375622413566095185632876940298382875066633305125595907908697818551860745708196640009079772455670451355426573609799907339222509103785567575227183775791345718826220455840965346196540544976439608810006794385963854831693077054723298130736781093200499800934036993104223443563872463385599425635345341317933466521378117877578807421014599223577201 |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |