Here’s the secret behind one of the mathematical tricks that Persi Diaconis, of Stanford University, developed for his magic act:
In order to guess the cards held by five audience members who have all taken consecutive cards from the top of the deck after the cards have been cut several times, the performer must arrange the cards in the deck carefully before starting the trick. In a way, the cards are arranged in a coded sequence of alternating red and black, which the performer can decode—although doing so takes some work.
Oh, and this trick works best with a deck of only 32 cards, rather than the standard 52. (In the book, Diaconis notes that spectators never seem to notice the thinner deck he has handed out.)
Because of a concept known as de Bruijn sequences, Diaconis was able to find an order of the cards so that there are 32 distinct arrangements of black cards and red cards, with no repetition of color order. The goal is to make it so that only one arrangement with any given order could be held by one of the five volunteers. That way, if the performer knows which spectators have the red cards and which have the black cards, he can determine which cards all of them hold.
Of course, memorizing 32 sequences would be difficult, so Diaconis would write the possible card orders on a cheat sheet and would pretend to scribble notes on that sheet while delivering banter about how difficult it is to read the minds of his audience members.
The arrangement of the cards, when coded into a binary sequence of zeros and ones (where 1 = red and 0 = black), can be decoded to determine the first card (so the performer knows where to start on the cheat sheet). The first two digits denote the suit of the card: 00 = club, 10 = diamond, 01 = spade, and 11 = heart. The last three digits, when converted from base two (binary) to the familiar base 10, reveal the card’s value; a three, for example, is represented by 0,1,1. So the sequence 1,0,0,1,1 means the first card is the three of diamonds.
Here is how to arrange the 32 cards to make the trick work:
8 of clubs, ace of clubs, 2 of clubs, 4 of clubs, ace of spades, 2 of diamonds, 5 of clubs, 3 of spades, 6 of diamonds, 4 of spades, ace of hearts, 3 of diamonds, 7 of clubs, 7 of spades, 7 of hearts, 6 of hearts, 4 of hearts, 8 of hearts, ace of diamonds, 3 of clubs, 6 of clubs, 5 of spades, 3 of hearts, 7 of diamonds, 6 of spades, 5 of hearts, 2 of hearts, 5 of diamonds, 2 of spades, 4 of diamonds, 8 of spades, and 8 of diamonds.