Becoming a ‘Mathemagician’: The 27 Card Trick

I love a deck of cards. It’s amazing to think about all the things that can be done with cards- from card games to building a house of cards to just shuffling mindlessly.

One of my absolute favourite things1 is learning and performing card tricks. There’s something deeply satisfying about sleight-of-hand tricks which can leave people convinced you’re reading their mind. I’ve always loved that moment of surprise (and often frustration!) when people realise they have no idea what just happened.

But today, I wanted to share a difference kind of card magic, something that involves less sleight of hand2 and more maths.

From 21 to 27 Cards

If you’ve ever been roped into watching a card trick, there’s a good chance you’ve seen the classic 21 card trick.

Here’s a quick recap:

  1. Deal 21 cards face up into three piles of 7 cards, one card at a time in a left-to-right order.
  2. Ask a volunteer to think of a card and tell you which pile their card is in (or if you don’t have a volunteer, think of a card yourself and keep track of which pile your card is in). This pile is now the chosen pile.
  3. Gather the piles together, and place the chosen pile in the middle of the other two.
  4. Repeat the steps above three times
  5. After the third iteration, deal down to the 11th card, and it will always be the volunteer’s card.

This card trick will work every single time.

But what if we wanted more control? What if, instead of always revealing the card in the 11th position, we could control it to any position we like? If you haven’t already, then grab a deck of cards and follow the steps below! This time, instead of 21 cards, we’ll use 27 (that’s three piles of 9 cards).

1. Ask your volunteer to pick a number between 1 and 27. For this example, let’s use 12.

2.Deal the cards face up into three piles of 9, one card at a time from left-to-right. Ask your volunteer to think of a card and tell you which pile their card is in.

Now, instead of always putting the pile with the their card in the middle, you’re going to choose where it goes based on the number picked by your volunteer. For the number 12, we’ll be following the placement sequence below:

  • 1st iteration: Bottom of the stack
  • 2nd iteration: Top of the stack
  • 3rd (and final) iteration: Middle of the stack

3.Deal the card for the second and third time, ensuring you’ve placed the chosen pile as described above at the end of each iteration.

4.Now for the reveal, at the end of your 3rd iteration, gather the piles and deal down to the chosen number (in our case 12), and turn over that card. If you’ve followed all the steps currently, this should be the volunteer’s chosen card!

That’s it! Three deals, a bit of controlled stacking, and you can put the volunteer’s chosen card at any chosen position between 1 and 27.


Before we dive in, I’d like you to pause for a second and think about these questions below:

  • Why is it three piles with 9 cards in them? What’s special about the number 27?
  • Why did we chose that order: (bottom, top, middle) for the number 12?
  • Could changing the order give us a different number?

Consider labelling the three possible positions for the pile with the chosen card being labelled as:

  • Top= 0
  • Middle = 1
  • Bottom = 2

In our example, the sequence of placement would correspond with the number: 2 0 1 (bottom, top, middle).

Working with Base 3

You’ve probably heard of binary, or base 2, where numbers can be represented using just two digits: 1s and 0s. Each position in binary represents a power of 2.

Similarly, the sequence of placement of the card above is determined using ternary (or base 3). This means numbers are represented using the digits: 0,1, and 2, and each position in a base-3 number represents a power of 3.

Going back to our example above, the base-3 number we had was 102. The digits are reversed here because we’re building from the least to the most significant digit (more on this below)

Converting this to decimal would give us the number:

(30 x 2) + (31 x 0) + (32x 1) = 9 + 0 + 2 = 11

323130
102

Starting at Index 0

You might wonder, the chosen position/number in our example was 12, so why have we got 11 from base-3? This is because the list of position we’re working with starts with index 03.

When we write numbers in any base, we build them using powers of that base, starting from 0. The rightmost digit is the least significant digit, and the leftmost is the most significant digit.

For example, in Base 10 (Decimal), the number 108 means:

(100 x 1) + (101 x 0) + (102x 1) = 11 = 100 + 0 + 8 = 108

102101100
108

Notice how the rightmost digit is multiplied by 100. We’re not used counting from 0, so saying ‘the 12th card’ is the same as ‘index 11’

So to answer the questions posed above, the 27 card trick is just a clever application of base-3.

  • We’re working with 3 piles, which gives us 3 choices for placing the pile with the chosen card: top (0), middle (1), or bottom, (2).
  • We do this 3 times, which means we’re building a 3-digit number in base-3, and that gives 33 =27 possible outcomes.
  • The number chosen is between 1 and 27, we subtract 1 from that to get a 0-based index, and convert that to a base 3 number (eg: 12 -> 11 -> 102)
  • That base-3 number gives us a sequence of placements, one digit per iteration4
  • At the end of each iteration, when the volunteer’s tells us which pile contains the card, we put it in the position specified by the digit.
  • After three iterations, the chosen card is exactly where we wanted!

Step back to 21

Before we get carried away, let’s take a step back. I never actually explained why the 21 card trick works in the first place. It looks similar to the 27 version, three piles, three rounds, some clever stacking, but the underlying principles are slightly different.

Each time we place the pile with the chosen card in the middle, it changes the position of the overall stack. Basically, each iteration reduces the possible positions the card could be in, and after three iterations, we guarantee the chosen card is the at the 11th position. This is best illustrated in the table below:

Iteration Possible positions
01-21
18-14
210-12
311

So while the 27 card trick uses base-3 to control the position of the card, the 21 card trick narrows the possible positions until there is only one left.

Generalising to base-n

The first time I saw the 27 card trick being performed, it raised an obvious question: What if we had more cards? In fact, I think this trick scales beautifully. So consider the following example where we have 64 cards5 to work with.

This number works really well6 because 43 = 64. This means we can:

  • Deal 64 cards left-to-right into 4 piles with 16 cards in each pile
  • Think of a card and pick a number between 1 and 64
  • Subtract 1, and convert that number to base-4
  • At the of each iteration, deal the cards into 4 piles and place the pile with the chosen card at the position from your base-4 digit (starting from the right)

By now, you’ve probably spotted the pattern: If you deal cards into n piles and repeat the process k times , you can control the position of any card within nk cards.

This trick would work for any base as long as the number of cards is a power of the number of piles.7

  • Pick a number (N) between 1 and nk for where you want the card to end up in
  • Subtract 1 from the number (N-1) to convert to 0-based index
  • Convert that number to base n, which gives us k digits
  • At the end of each deal when we know which pile the card is in, stack the piles in the sequence generated by the digit
  • After k deals, the chosen card will be exactly where you wanted it!

Side-quest for the future

While writing this post, I really wish I had an interactive version of this card trick so that I could:

  • Choose values for n and k
  • Generate a virtual deck with nk cards
  • Pick any card and target position
  • Track the position of the card as it moves through the piles across each iteration, with the base-n digits highlighted.

It’s something I’ve been wanting to build myself, and I think it would be an excellent tool for understanding number bases and explore modular arithmetic, or just be a fun exercise for spotting a pattern. For the time being, I’ve added it to my every growing side-quests list.

If I build one (which is likely once I get around to learning Manim8), I’ll definitely link it here, and if you’re interested in collaborating, I’d love to hear from you!


  1. Which no doubt includes playing a few rounds of Racing Demons ↩︎
  2. And in this way I’m also not technically breaking the magician’s code… ↩︎
  3. This is also why 0-based index is used in programming. You can read more about it here. ↩︎
  4. Remember the least significant digit first – so 102 means our sequences (right to left) is bottom, top, middle. ↩︎
  5. A standard deck of cards has 52 unique cards (+2 jokers), but for this example, we can assume that the number of cards and the time taken to deal with are negligible constraints. If you want to try this out, grab another deck of cards so that you have 64 cards in total. ↩︎
  6. It’s also another cube number and apparently a good age to be… ↩︎
  7. Sadly.. this means we can’t really do this with exactly 52 cards as it isn’t a power of any integer. On that note, how can we check if a number is a power of another number or not? ↩︎
  8. Really useful Python library for animations, was initially developed by Grant Sanderson (3b1b) ↩︎

This post was inspired by a brilliant Numberphile video featuring Matt Parker performing this card trick. You can find the video linked below:

Leave a comment