## Perfect Shuffle

### March 13, 2020

A perfect shuffle, also known as a faro shuffle, splits a deck of cards into equal halves (there must be an even number of cards), then perfectly interleaves them. Eventually a series of perfect shuffles returns a deck to its original order. For instance, with a deck of 8 cards named (1 2 3 4 5 6 7 8), the first shuffle rearranges the cards to (1 5 2 6 3 7 4 8), the second shuffle rearranges the cards to (1 3 5 7 2 4 6 8), and the third shuffle restores the original order (1 2 3 4 5 6 7 8).

Your task is to write a program that performs a perfect shuffle and use it to determine how many perfect shuffles are required to return an *n*-card deck to its original order; how many perfect shuffles are required for a standard 52-card deck? When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Cool drill. Here is my take on it using Julia: https://pastebin.com/ppSpZ5BH

For a 52 card deck, it takes just n = 8 perfect shuffles, while for a 54 card one (one which includes 2 jokers) it takes many more.

Have a nice weekend and stay healthy!

In Python.

Here is a simple solution in R7RS Scheme and a few popular helpers.

It includes a formulaic method for computing the cycle-length in

addition to the explicit one.

Output:

Looking at the pattern of numbers, it seems it is sometimes possible to calculate the number of perfect shuffles with an equation instead of actually doing the perfect shuffles.

For an even number N and N-1, they will both have the same answer.

E.g. 52 and 51 both take 8 perfect shuffles

I’ve also noticed for any number that is 2^n, the number of perfect shuffles is n.

E.g. 8 = 2^3, n=3, so the number of perfect shuffles is 3.

For any number that is (2^n) + 2 or 2^n + 1, the answer is 2n.

E.g. 10 = (2^3)+2, n = 3. The answer is 2n = 6.

Also: 9 = (2^3)+1, n = 3. The answer is 2n = 6.

Unfortunately, I can’t figure out the pattern beyond that, but in these special cases, the number of perfect shuffles can be found in O(1) time.

If there is an equation to describe the entire pattern, perhaps all cases could be solved in O(1) time.

Code:

import numpy as np

import math

#In some cases, we do not need to actually perform the perfect shuffle: we can determine the answer with simple math

def checkSpecialCases(arr):

#An even number N will have the same solution as N-1

#Add 1 to odd numbers to make checks easier

N = len(arr) + len(arr)%2

def perfectShuffle(arr):

orig = np.copy(arr)

if

name== ‘main‘:for i in range(2,9):

arr = np.arange(1,i+1)

print(str(i) + ‘ : ‘ + str(checkSpecialCases(arr)))

Output:

2 : 1

3 : 2

4 : 2

5 : 4

6 : 4

7 : 3

8 : 3

Quoting Programming Praxis :

Further;If the aim of shuffling is to make the card order unpredicatable or at least difficuld to predict; how many “faro-shuffles” of a 52-card stack is ideal ?

> programmingpraxis posted: “A perfect shuffle, also known as a faro > shuffle, splits a deck of cards into equal halves (there must be an > even number of cards), then perfectly interleaves them. Eventually a > series of perfect shuffles returns a deck to its original order. For > instance,” > > > T

Here’s a solution in C.

Example:

As indicated on Wikipedia, this matches sequence A002326 on OEIS.

Here’s a Haskell version. Instead of shuffling an actual list I use the formulas from the “Faro shuffle” Wikipedia page.

I had fun with this one:

Klong version

Klong version

Sorry for lack of formatting, new to the site. This is a vanilla Python solution; I was stumped for a bit but figured it out pretty quickly after drawing out a couple of cards and seeing how they moved.

Number of shuffles for 52 cards: 8

for i in range(2, 14, 2):

# Initialize deck size

deck_size = i

if deck_size % 2 != 0:

msg = “Deck size must be even”

raise ValueError(msg)

=====================================

OUTPUT

For a deck of 2 cards, it takes 1 perfect shuffles to return to its original permutation.

For a deck of 4 cards, it takes 2 perfect shuffles to return to its original permutation.

For a deck of 6 cards, it takes 4 perfect shuffles to return to its original permutation.

For a deck of 8 cards, it takes 3 perfect shuffles to return to its original permutation.

For a deck of 10 cards, it takes 6 perfect shuffles to return to its original permutation.

For a deck of 12 cards, it takes 10 perfect shuffles to return to its original permutation.

@Sam – to format code, put ‘[code lang=”python”]’ on a line at the start of the block of code, and ‘[/code]’ on a line at the end.

Just to be clear – don’t include the single quotes there, just the square brackets and the stuff inside:

Racket: https://github.com/xojoc/programming-challenges/blob/master/programming-praxis/2020_03_13.rkt

Further;If the aim of shuffling is to make the card order unpredictable or at least as unpredictable as possible ;

how many “faro-shuffles” of a 52-card stack is ideal ?….sorry about such a late comment , it was stimulated by the current array shuffling problem even though it has no relation to it !