algorithm explanation for card trick I have been studying algorithms for a project im developing, and found this card trick,
can any matemathical guru help me understand the algorithm behind??
card trick
thanks a lot!
 A: This is not that difficult to understand, but perhaps the idea is new to you. 
You are dividing the deck into 3 parts and they add up to 52 cards. Also, note that adding the digits of a number till you are left with only one number is the same as looking at the number modulo 9. That is, considering the remainder when the number is divided by 9. 
So, you start with the deck having a value of 7. (52 cards so 5+2=7).
Then you have some number of cards in your hand, say 26. Then the value of your hand is 2+6=8. But you cannot take away 8 from 7 so you take away 8 from the next number that has a sum 7. That number is 16. So, 16-8=8. So, the remaining cards must have a value of 8. because then their 8 and your 8 add up to 16 which is 7. 
A: This is a simple application of "casting out nines".
The sum of the digits of any base ten number, $n$, is equal to $n\pmod{9}$.  This is because $10^k=1\pmod{9}$.  This is easy to see: take any power of $10$, $10^k$ and subtract $1$; you get a string of $k$ $9$s.  Any base ten number, $n$, can be written as the following weighted sum of its digits, $d_k$
$$
n = \sum_k\;d_k10^k
$$
$s$, the sum of the digits of $n$, is given by
$$
s=\sum_k\;d_k
$$
Thus, the difference of $n$ and $s$ is
$$
n-s = \sum_k\;d_k(10^k-1)
$$
where each term has a factor of $10^k-1$ which is divisible by $9$.  Therefore, the difference of a number and the sum of its digits is divisible by $9$.  That is, $s=n\pmod{9}$.  Thus, by repeatedly adding the digits of $n$ until there is only one digit left, you have found a number from $1$ to $9$ which is equal to $n\pmod{9}$.
The card trick works because the number of cards in the deck is $52=7\pmod{9}$.  This means that the sum of the number of cards in all three piles must be $7=16\pmod{9}$.
If $s$, the number of cards in the dealers hand, is from $1\mathrm{\ to\ }6\pmod{9}$, then the sum of the  number of cards in the other two hands will be $7-s\pmod{9}$.  If $s$ is from $7\mathrm{\ to\ }9\pmod{9}$, then the sum of the  number of cards in the other two hands will be $16-s\pmod{9}$.
