How does the 27 card trick exactly work?

When you write a number in ternary, it’ll be in the form cₙcₙ₋₁…c₁c₀ (where each c can be 0,1, or 2). In regular base-10 representation, this is equal to:
> cₙ(3ⁿ) + cₙ₋₁(3ⁿ⁻¹) +…+ c₁(3¹) + c₀(3⁰)

Here are a few examples:
> 1000 = 1(3³)+0(3²)+0(3¹)+0(3⁰) = 27

> 222 = 2(3²)+2(3¹)+2(3⁰) = 18+6+2 = 26

> 102 = 1(3²)+0(3¹)+2(3⁰) = 9+0+2 = 11

Now, let 0 correspond to the top pile, 1 to the middle pile, and 2 to the bottom pile. You ask the person to choose a number k between 1 and 27 — this is going to be the position of their card at the end. Since their card is going to be the kth card in the pile, that means there are k-1 cards on top of their card.

Convert k-1 into ternary, using 3 digits (meaning that if the ternary representation has only 1 or 2 digits, you add 0’s at the beginning). The digit on the right tells you where to put the pile after the first deal, the second digit tells you where to put the pile after the second deal, and the first digit on the left tells you where to put the pile after the last deal. Finally, start at top card and count out k cards. The kth card will be theirs.

——

**Example 1:** suppose the person chooses 15. You want their card to wind up being the 15th in the pile, meaning you want there to be 15-1=14 cards *before* (on top of) their card.
> 14 = 1(3²)+1(3¹)+2(3⁰) = **112**

Deal out the cards one by one into 3 separate piles, face up. When you’re done, have them choose which pile their card is in. The digit on the right is 2, which means bottom. Stack the three piles, placing the pile with their card on the bottom.

Once again, deal out the cards into 3 piles face-up. The next digit is 1, which corresponds to the middle. After they tell you which pile their card is in, stack the piles and place theirs in the middle.

The digit on the left is also 1. Repeat the previous step, once again placing their pile in the middle.

Finally, count out 14 cards and discard them. The 15th card is theirs.

——

**Example 2:** They choose 8. There will be 8-1=7 cards on top of theirs.
> 7 = 2(3¹)+1(3⁰) = 21

> This is only two digits, so add a 0 at the beginning: **021**.

> 1 = middle, 2 = bottom, 0 = top

After the first deal, place their pile in the middle. After the second deal, place their pile on the bottom. After the third deal, place the pile with their card on top. Discard the first 7 cards, then flip over the 8th card - it’s theirs.

* *Note: the flip the piles of cards back over as you combine the deck. So if their card is on the bottom, that’s the bottom of the deck facing down.*
The 27 card trick exactly works with 21 cards, not 27.
I think I may be a bit of the way there. So if you think of top, top, top being the order you do to make your card in the first position, and bottom,bottom,bottom being the order you use to get your card at the 27 th position, then changing the order of top, top, top, and bottom, bottom,bottom gets you the orders between.

So middle, top, top gets you second position and so on.

Top, middle and bottom can be exchanged for 0,1,2.

If you think of the first position being where you have cards in piles 000000000, 111111111,222222222, then you equate this first position to being the units of ternary numbers, so that's why you get middle, top, top, not top,top, middle.

I found trying to map the way the cards are divided each time, and relate that to different positions was too confusing for me, so this makes more sense. But if someone was able to do this and could see why the first group was thought of as the units column, it would be really helpful.

0 like 0 dislike