Jump to content

Perfect shuffle: Difference between revisions

Added solution for Action!
(add FreeBASIC)
(Added solution for Action!)
Line 124:
1024 | 10
10000 | 300
</pre>
 
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<lang Action!>DEFINE MAXDECK="5000"
 
PROC Order(INT ARRAY deck INT count)
INT i
 
FOR i=0 TO count-1
DO
deck(i)=i
OD
RETURN
 
BYTE FUNC IsOrdered(INT ARRAY deck INT count)
INT i
 
FOR i=0 TO count-1
DO
IF deck(i)#i THEN
RETURN (0)
FI
OD
RETURN (1)
 
PROC Shuffle(INT ARRAY src,dst INT count)
INT i,i1,i2
 
i=0 i1=0 i2=count RSH 1
WHILE i<count
DO
dst(i)=src(i1) i==+1 i1==+1
dst(i)=src(i2) i==+1 i2==+1
OD
RETURN
 
PROC Test(INT ARRAY deck,deck2 INT count)
INT ARRAY tmp
INT n
 
Order(deck,count)
n=0
DO
Shuffle(deck,deck2,count)
tmp=deck deck=deck2 deck2=tmp
n==+1
Poke(77,0) ;turn off the attract mode
PrintF("%I cards -> %I iterations%E",count,n)
Put(28) ;move cursor up
UNTIL IsOrdered(deck,count)
OD
PutE()
RETURN
 
PROC Main()
INT ARRAY deck(MAXDECK),deck2(MAXDECK)
INT ARRAY counts=[8 24 52 100 1020 1024 MAXDECK]
INT i
 
FOR i=0 TO 6
DO
Test(deck,deck2,counts(i))
OD
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Perfect_shuffle.png Screenshot from Atari 8-bit computer]
<pre>
8 cards -> 3 iterations
24 cards -> 11 iterations
52 cards -> 8 iterations
100 cards -> 30 iterations
1020 cards -> 1018 iterations
1024 cards -> 10 iterations
5000 cards -> 357 iterations
</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.