I have found the following card trick:

- Take a stack of
**n**cards, with a known order. - Deal the cards from your hand onto the table like this: Card from the top, then card from the bottom, from the top, from the bottom, etc until the deck is depleted.
- Keep dealing the cards like this. After a certain amount of deals (lets call that
**d**) the original order will be restored.

I have made a table for n=1 through 25. The numbers for d are, consecutively:
`1,2,2,3,3,5,6,4,4,9,6,11,10,9,14,5,5,12,18,12,10,7,12,23,21`

I'm trying to find a pattern here, but I can't find one. My first thought was that after the same amount of deals as the amount of cards the sequence would repeat. Some seem to conform to n-1=d, but most don't. If you check for n-1=z*d, where z is an integer, most of them conform, but some don't.

Does anyone know what logic is hidden here? What is so special about n=16 and 17 (both just 5 deals required) and n=22 (just 7 deals required)?