My brother sent me this video of a nice magic card trick and asked me to figure out the maths behind it.
If you can't/don't want to watch the video, it works like this:
Here's how it works.
Steps 6 through 8 are deterministic and you always play them out in the same way, so they will always reveal the card that is in the 22nd position of the deck, regardless of whatever happened in steps 1 through 5. Here's a simulation in Python:
deck = list(range(1, 53))
while len(deck) > 1:
deck = list(reversed(deck[1::2]))
print(deck) # [22]
So, it's just a matter of understanding that steps 1 through 5 always make you look at the 22nd card.
Suppose the cards shown have the values \(a\) and \(b\). On top of the card that is worth \(a\), you discard \(10 - a\) cards. On top of the card that is worth \(b\), you discard \(10 - b\) cards. Finally, you look at the card at position \(a + b\).
But when you put everything back on top, the card that was in position \(a + b\) is now under some extra cards:
Adding those up gives \(22 - a - b\) cards. So, the card that was in position \(a + b\) of the partial deck is in position \((22 - a - b) + (a + b)\) of the full deck, which simplifies to 22. So, you're always looking at the 22nd card.
Try it for yourself. Grab the ace of spades and make sure it's the 22nd card counting from the top of the deck. Play these steps out and you'll always find the ace of spades.