Understanding Gergonne's Card Trick - Python implementation and underlying mathematics

I have recently watched an amazing video from Numberphile about a card trick. The card trick in the video was a nice extension of 21 card trick, which I knew since I was a kid. To be honest, I was fascinated by how math was used in the trick to move a selected card to any chosen position in the deck of 27 cards. However, the explanation in the video of underlying mathematics was cursory and unconvincing. And because of that, I have decided to write this post and explain how the trick works and how it can be generalized to other deck sizes.

Read More