What is mathematically significant about this algorithm? In this question, a certain algorithm was presented:

Start with a deck of $n$ cards.
  
  
*
  
*Take the top card off and set it on the table on top of any cards already there
  
*Move the new top card to the bottom of the deck
  
*Go to step 1 if there are still cards in the deck. When there are no more cards in the deck, one round has passed
  
*Pick up the cards from the table; this is the new deck. If the new deck is in the original order, we are done. Otherwise, repeat from step 1.

As I show in my answer to that question, there are a few peculiarities.


*

*Powers of two go through few rounds before the cards are in the original order. For example, while the numbers around $64$ take $60+$ rounds before returning to their original state, $64$ takes only $12$ rounds. It appears that every power of two other than two results in $4m$ rounds where $m\in\Bbb N$.

*Prime numbers go through an unusually large number of rounds. For example, while the numbers around $31$ all take less than $100$ rounds, $31$ takes $210$ rounds.

*There are doubtless other interesting properties of this algorithm


What is significant about this algorithm that causes prime numbers and powers of two to have peculiar output?

In response to Victor Engel's comment, here is a proof that the algorithm will always return the deck to the starting case:


*

*We know that each round is a one-to-one function from a deck ordering to another deck ordering (this should be clear from the way it is set up; it is reversible).

*We know that there are a finite number of deck orderings.


Assume that there is a case where the algorithm does not go back to the starting ordering.  
$\implies$ In that case, either there are an infinite number of orderings, or there is a loop somewhere in the sequence.
But we know that there are a finite number of orderings (see 2.).
$\implies$ there is a loop somewhere in the sequence.
$\implies$ either it loops back to the starting ordering or it loops to another ordering in the sequence.
But we know that it doesn't loop back to the starting ordering because that contradicts our assumption that it doesn't.
Thus, the sequence loops to another ordering in the sequence.
$$\implies\Longleftarrow$$
This is a contradiction. In order for the sequence to loop to another ordering in the sequence, the function would have to be not one-to-one because two elements in the domain would map to the same element in the range.
$\therefore$ We conclude that repeated applications on the algorithm always leads back to the original case.
As a side note, this same proof works for proving that the repeated application of a certain algorithm on a rubix cubes always leads back to the original rubix cube
 A: Partial answer: Here is powers of two:
Let's investigate the first few powers of two. Let the left-to-right order denote the top-to-bottom permutation of the cards. Here goes:
$\{ a_1, a_2\} \rightarrow \{a_2, a_1\}$
$\{a_1, a_2, a_3, a_4\} \rightarrow \{a_4, a_2, a_3, a_1\}$
$\{a_1, a_2, a_3, a_4, a_5, a_6, a_7, a_8\} \rightarrow \{a_8, a_4, a_6, a_2, a_7, a_5, a_3, a_1\}$
In this last case, there are a few things going on:
First cycle: $1 \rightarrow 8 \rightarrow 1$
Second cycle: $2 \rightarrow 4 \rightarrow 2$
Third cycle: $3 \rightarrow 7 \rightarrow 5 \rightarrow 6 \rightarrow 3$
So to find the number of cycles required, find the LCM of the cycle lengths.
This is how you would make sense of the pattern: If we read right to left, first we deal with the odds in an increasing order, and then our set of $2^n$ cards is reduced to a set of $2^{n-1}$ cards in the form of $\{2, 4, 6, \dots, 2^n\}$. So then, the "odds" of this set (congruent to $2 \pmod 4$) are picked out, giving us $2^{n-1}$ cards in the form $\{4, 8, 12, \dots, 2^n\}$. Then $4 \pmod 8$ are picked out in increasing order and so forth until we run out of cards. I'm not sure yet if we can make a closed form generalization for any $2^n$ cards (though it's apparent that the first and last cards will switch places).
Another example:
$\{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16\} \rightarrow \{16, 8, 12, 4, 14, 10, 6, 2, 15, 13, 11, 9, 7, 5, 3, 1\}$
$1 \to 16$
$2 \to 8$
$3 \to 15 \to 9 \to 12$
$4 \to 4$
$11 \to 11$
$5 \to 14$
$6 \to 7 \to 13 \to 10$
Again, the number of cycles would be $4$.
In a deck of $2^n$ cards, the numbers $2^{n-1}$ and $2$ swap positions, as well as the numbers $2^n$ and $1$.
Some examples of prime numbers:
$\{1, 2, 3, 4, 5, 6, 7\} \rightarrow \{6, 2, 4, 7, 5, 3, 1\}$
Which gives the permutation $(17436)$
$\{1, 2, 3, 4, 5, 6, 7, 8, 9, A, B\} \rightarrow \{6, A, 2, 8, 4, B, 9, 7, 5, 3, 1\}$
(For the sake of cycle notation I am using $A = 10, B = 11$)
Which gives the permutation $(1B6)(23A)(45978)$, so the required number of cycles would be $3 \times 5 = 15$
