Solution behind a card trick?

I just saw a new card trick tonight and I'm sure there's a mathematical solution to it....just wondering if anyone out there has seen it before. The trick goes like this:

1. Start with a 52-deck of cards

2. Flip the first card over; the number value of the card is the number that you "start counting with", and you count out the number of cards after that so that you end at 13. (For example, the first card you flip is a 9-card...you count out 4 more cards to "count" to 13...the proceeding 4 accounting for 10, 11, 12, and 13....and ultimately you have a little pile of 5 cards). When you are done counting that pile of cards, you put that pile - face down - aside.

3. Continue this process until the entire deck is in little piles of cards....however many it takes.

4. Pick up all but 3 piles of cards, so that all you have left are 3 face-down piles of cards. Flip the top card on any 2 of the piles.

5. Add the values of the 2 newly-flipped cards, and add 10.

6. In the remaining pile (the rest of the deck of cards that you picked up in step 4), count out the number of cards that you ended up with in step 5 - the sum of the values of the 2 cards that you flipped up plus 10.

7. The number of cards remaining in your hand are the same as the value of the "3rd" pile card that is still facing down. (So, for example, if in step 6 you have a 3 and a 5 facing up - you will have counted out 18 cards, and if you have 7 cards remaining in your hand, you can expect that last card to be a 7).

....VERY cool trick. Has anyone seen it and does anyone know why that works? I'm assuming that the magic number 13 relates somehow to the number of cards for a particular suit in the deck....or maybe, alternatively, it's based on the number 4 and 13 is the divisor that gets you there.

I'm sure this one will be swimming around in my head for awhile and I'm anxious to understand the answer...just pretty rusty in actually applying math, and off the bat if this has anything to do with linear algebra (which I know a lot of game theory is based on), I've never been overly strong in that area.

2. Flip the first card over.
The number value of the card is the number that you "start counting with",
and you count out the number of cards after that so that you end at 13.
(For example, the first card you flip is a 9.
You count out "Ten, eleven, twelve, thirteen" and deal 4 cards on top of the 9.
When you are done with that pile of cards, you place it aside, face down.

3. Continue this process until the entire deck is in small piles of cards.
Place the leftover cards ("discards") to the side.

4. Pick up all but 3 piles of cards and add them to the discards.
You have 3 face-down piles of cards lfet.
Flip the top card on any 2 of the piles.

5. Add the values of the 2 newly-flipped cards, and add 10.

6. From the discards, count out the number of cards that you ended up with in step 5
(the sum of the values of the 2 cards that you flipped up plus 10.)

7. The number of cards remaining in your hand is equal to
the value of the top card of the last pile.

VERY cool trick. Has anyone seen it and does anyone know why that works?
I assume that the magic number 13 relates somehow to the number of cards in a suit.

Suppose we make five piles.
Suppose further that the five key cards have values:

So the first card is and we deal cards on top of it.. . The pile has cards.
The second card is and we deal cards on it.. . The pile has cards.
The third card is and we deal cards on it.. . The pile has cards.
The fourth card is and we deal cards on it.. . The pile has cards.
The fifth card is and we deal cards on it.. . The pile has cards.

We have used: cards.

Hence, there are: . "discards".

Select any three piles . . . say, the last three.
We will add those cards to the discards.
The discards will have:. . cards.

Select any two of the remaining three piles . . . say, the last two.
We add their key cards, , and add ten:

We deal that many cards from the discards.
Then the discards will contain: .

Makes sense!

Thank you for your responses on this - they make sense when I see them all laid out.

For the first response, there was some concern about the "last pile" possibly not having 14-x_k cards in it, and therefore that last case would not work. I checked back with the person who showed me the trick, and he told me that in that case (if you can't count up to 14 with the remaining cards), then you don't put that pile down...you hold it in your hand. So, that takes the "last pile" case out of the equation.