In-Shuffle

Author

Eric W. Weisstein
March 22, 2006

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Games/In-Shuffle.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/In-Shuffle.html.

©2006 Wolfram Research, Inc. except for portions noted otherwise

Initialization

For odd n, need

Partition[Range[n],n/2,n/2,1,{}]

But for even n,

Shuffle[l_List]:=Transpose[Partition[l,Length[l]/2]]/;EvenQ[ Length[l]]
Shuffle[n_Integer]:=Shuffle[Range[n]]

InShuffle[l_List]:=Flatten[Reverse/@Shuffle[l]]/;EvenQ[ Length[l]]
OutShuffle[l_List]:=Flatten[Shuffle[l]]/;EvenQ[ Length[l]]

In-Shuffle

8 cards

InShuffle[8]

With[{n=8},Table[Mod[2k,n+1],{k,n}]]

<<DiscreteMath`Combinatorica`

InversePermutation[{2,4,6,8,1,3,5,7}]

Deck  of 52 cards after multiple inshuffles

Select[Range[2,12,2],PrimeQ[#+1]&]

Function[n,{n,Nest[InShuffle,Range[n],n]}]/@Select[Range[2,12,2],PrimeQ[#+1]&]//ColumnForm

Function[n,Nest[InShuffle,Range[n],n]==Range[n]]/@Select[Range[2,100,2],PrimeQ[#+1]&]

And@@%

Shuffles to return deck to original state

Length[NestWhileList[InShuffle,Range[52],UnsameQ,All]]-1

Table[Length[NestWhileList[InShuffle,Range[n],UnsameQ,All]]-1,{n,2,100,2}]

Table[MultiplicativeOrder[2,2n+1],{n,50}]

Deck of 52 cards after 1 shuffle

<<Utilities`EISFormat`

FormatSequence[InShuffle[52],59952,1,
Name->"Ordering of a deck of 52 cards after an in-shuffle.",
Cross->{59953},
html["RiffleShuffle"],
Keyw->{"easy","fini"}
]