Gergonne's Piles 3 - The L2 Formula and Magic Cases

Posted by Pietro Vitelli on November 07, 2026

A full nine years after the previous in-depth article (in Italian), here we are talking about Gergonne’s piles again.

Let’s clear one thing up right away: the credit for returning to this topic definitely goes to my friends “Claudio” and “Chad”, who somehow managed to overcome my now almost pathological reluctance to get back to doing calculations. :)

We left off with the conjecture that the formula $L_2$ could be used for all magic cases.

The idea came from comparing, for a few configurations of the trick, the general formula derived from the work of Harrison et al. 1 2 with a much simpler version:

\[L_2 = 1+\frac{\sum_{i=1}^{m} k_i n p^{i-1}}{p^m}.\]

The results obtained with the simulator seemed to support this intuition. However, those checks covered only a limited number of configurations: we still needed to establish whether the formula really worked for all magic cases and, above all, why.

We can now confirm that it does: in magic cases, the final position of the card is always given by the floor of $L_2$.

But there is more. The formula works not only in fully magic cases, meaning configurations in which every possible sequence leads to a uniquely determined final position.

It also holds for individual magic sequences: those particular sequences of pile reassemblies for which the card’s final position does not depend on its initial position $x$, even when the configuration as a whole is not magic.

This is not merely a lucky approximation, but a consequence of the exact formula describing the reassemblies.

Starting from this proof, we will also derive a criterion for identifying magic sequences, a condition for determining when all sequences are magic, and a formula for counting them for a given configuration.

Back to the formula $L_2$

We will keep the notation used in the two previous articles:

  • $n$ is the number of cards in each pile;
  • $p$ is the number of piles;
  • $m$ is the number of reassemblies;
  • $k_i$ is the position in which the pile containing the chosen card is placed, counting from $0$ to $p-1$ from the top of the deck;
  • $x$, with $1\leq x\leq n$, is the card’s initial position within the chosen pile.

As in the second article, we consider a single execution of the pattern, that is, $\ell=1$. After the $m$ reassemblies, we deal the cards once more and observe the card’s position in the pile indicated by the spectator.

To simplify the calculations, let:

\[t=\sum_{i=1}^{m} k_i p^{i-1}=k_1+k_2p+\cdots+k_mp^{m-1}.\]

The formula proposed in the previous article then becomes:

\[L_2=1+\frac{nt}{p^m}.\]

The card’s actual position

Consider a single reassembly. If the card is in position $x$ within its pile, and that pile is placed after $k$ other piles, its new position within its pile after the next deal will be:

\[f_k(x)=1+\left\lfloor\frac{x-1+kn}{p}\right\rfloor.\]

The symbol $\lfloor z\rfloor$ denotes the floor of $z$, the greatest integer less than or equal to $z$.

Applying the transformation $m$ times, using the positions $k_1,\ldots,k_m$, gives:

\[f_m(x)=1+\left\lfloor\frac{x-1+nt}{p^m}\right\rfloor. \tag{1}\]

This is the exact formula for the final position. Unlike $L_2$, it also contains $x$, the initial position that the magician does not know.

Proving that $L_2$ works in magic cases

We want to prove that, for any magic sequence, the card’s final position is always given by:

\[f_m(x)=\left\lfloor L_2\right\rfloor.\]

A sequence is magic when the card’s final position is independent of its initial position. In mathematical terms, this means:

\[f_m(1)=f_m(2)=\cdots=f_m(n).\]

Therefore, if the sequence is magic, we can calculate the final position using any value of $x$, since the result will always be the same.

Let us choose the simplest value: $x=1$.

Substituting into the exact formula (1), we obtain:

\[f_m(1)=1+\left\lfloor\frac{1-1+nt}{p^m}\right\rfloor.\]

Simplifying:

\[f_m(1)=1+\left\lfloor\frac{nt}{p^m}\right\rfloor.\]

Recalling that, for any real number $a$:

\[1+\lfloor a\rfloor=\lfloor1+a\rfloor,\]

we can write:

\[f_m(1)=\left\lfloor1+\frac{nt}{p^m}\right\rfloor.\]

But the expression inside the floor is precisely $L_2$. Thus:

\[f_m(1)=\left\lfloor L_2\right\rfloor.\]

Since we assumed that the sequence is magic, we know that $f_m(x)=f_m(1)$ for every $x$ between $1$ and $n$.

We therefore obtain:

\[f_m(x)=\left\lfloor L_2\right\rfloor\qquad\forall x\in\{1,\ldots,n\}.\]

We have thus proved that the formula $L_2$ always gives the correct final position of the card for any magic sequence, regardless of the values of $n$, $p$, $m$, and the individual $k_i$.

It is interesting to note that $L_2$ is not simply an approximation of the final position: its floor is exactly the final position calculated for $x=1$.

In magic cases, because the final position is independent of $x$, this result automatically holds for every possible initial position of the card.

This confirms the intuition expressed at the end of the previous article. Of course, it does not mean that all sequences are magic: for a non-magic sequence, the final position may still depend on $x$.

How to identify a magic sequence

At this point, a natural question arises: can we tell in advance whether a given sequence is magic, without performing the trick and without knowing $x$?

The answer is yes, and we can derive the criterion directly from formula (1).

Since $f_m(x)$ is a non-decreasing function of $x$, it is constant for all $x$ between $1$ and $n$ if and only if:

\[f_m(1)=f_m(n).\]

Substituting into (1):

\[\left\lfloor\frac{nt}{p^m}\right\rfloor=\left\lfloor\frac{nt+n-1}{p^m}\right\rfloor.\]

Let $q=p^m$ and write the integer division of $nt$ by $q$ as:

\[nt=aq+r,\qquad 0\leq r<q.\]

The value $r$ is the remainder, which we will denote by $nt\bmod q$. The previous equality becomes:

\[a=a+\left\lfloor\frac{r+n-1}{q}\right\rfloor.\]

This holds if and only if:

\[r+n-1<q.\]

Since these are integers, we obtain our first criterion:

\[(nt\bmod p^m)+n\leq p^m. \tag{2}\]

A sequence of reassemblies is magic if and only if it satisfies (2).

An example: 8 cards per pile, 6 piles

Let us return to the $8\times6$ configuration, with $n=8$, $p=6$, and $m=2$, which we studied in the second article.

Consider the sequence $(k_1,k_2)=(0,2)$. We have:

\[t=0+2\cdot6=12,\qquad p^m=36.\]

Since:

\[(8\cdot12)\bmod36=24,\]

condition (2) becomes:

\[24+8=32\leq36.\]

The sequence is magic, and the final position is:

\[\lfloor L_2\rfloor=\left\lfloor1+\frac{8\cdot12}{36}\right\rfloor=3.\]

Now let us try the sequence $(k_1,k_2)=(1,2)$:

\[t=1+2\cdot6=13,\qquad (8\cdot13)\bmod36=32.\]

This time:

\[32+8=40>36.\]

The sequence is not magic: depending on its initial position $x$, the card may end up in position $3$ or $4$.

This is an important distinction compared with the 2017 analysis: a configuration can contain many magic sequences without all of its sequences being magic.

When every sequence in a configuration is magic

So far, we have examined one sequence at a time. But we can also ask when a configuration $(n,p,m)$ makes every choice of $k_1,\ldots,k_m$ magic.

We will call such a configuration universally magic.

The result is particularly simple:

\[n\mid p^m. \tag{3}\]

The symbol $\mid$ means “divides”: in other words, $p^m$ must be a multiple of $n$.

Proof

Again, let $q=p^m$, and let:

\[g=\gcd(n,q)\]

be the greatest common divisor of $n$ and $q$.

As $t$ ranges from $0$ to $q-1$, the possible remainders of $nt$ divided by $q$ are exactly:

\[0,g,2g,\ldots,q-g.\]

For every sequence to be magic, criterion (2) must hold even for the largest remainder, $q-g$:

\[q-g+n\leq q,\]

or equivalently, $n\leq g$. But we also know that $g\leq n$, so we must have $g=n$, which is precisely equivalent to $n\mid q$.

For example, in the $8\times4$ configuration with $m=2$:

\[p^m=4^2=16,\qquad 8\mid16.\]

All $16$ sequences are magic. The final position depends on the chosen sequence, but not on the card’s initial position.

Counting the magic sequences in a configuration

We now come to what may be the most interesting question. When a configuration is not universally magic, how many of the $p^m$ possible sequences still work?

In the previous article, I tackled this problem by constructing tables with the simulator. But we can obtain the exact number directly from a formula.

First, assume that $n\leq p^m$ and, as before, let:

\[q=p^m,\qquad g=\gcd(n,q).\]

A sequence is magic when the remainder $r=nt\bmod q$ satisfies:

\[r\leq q-n.\]

We know that the possible remainders are the multiples of $g$ between $0$ and $q-g$, and that each remainder occurs exactly $g$ times as $t$ ranges from $0$ to $q-1$.

Since $g$ divides both $q$ and $n$, it also divides $q-n$. The admissible remainders are therefore:

\[0,g,2g,\ldots,q-n.\]

Their number is:

\[\frac{q-n}{g}+1.\]

Multiplying by $g$, we obtain the number $M$ of magic sequences:

\[M=g\left(\frac{q-n}{g}+1\right)=q-n+g.\]

Substituting the definitions of $q$ and $g$:

\[M=p^m-n+\gcd(n,p^m). \tag{4}\]

This formula holds when $n\leq p^m$. If $n>p^m$, no sequence is magic: criterion (2) cannot be satisfied.

Comparing a few cases


$n$ $p$ $m$ Possible sequences Magic Non-magic
8 4 2 16 16 0
8 6 2 36 32 4
10 4 2 16 8 8
13 4 2 16 4 12
13 4 3 64 52 12
8 3 2 9 2 7
10 3 2 9 0 9


A full deck of 52 cards

Take a deck of $52$ cards and deal it into $4$ piles of $13$ cards:

\[n=13,\qquad p=4.\]

With two reassemblies, we have $4^2=16$ sequences. Since $\gcd(13,16)=1$:

\[M=16-13+1=4.\]

With three reassemblies, the number of possible sequences becomes $4^3=64$, while:

\[M=64-13+1=52.\]

Thus, 52 out of 64 sequences, or $81.25\%$, are magic. There are still only $12$ non-magic sequences, exactly as in the case with two reassemblies.

This does not mean that the magician can always locate the card by choosing a sequence at random: it means that, when the sequence used is known, in $81.25\%$ of cases it determines a final position that is independent of the initial position.

Conclusions

Starting from the final observation in my 2017 article, we have now clarified the role of the formula $L_2$.

Its floor is not merely an approximation of the final position: it is exactly the card’s position for every magic sequence, that is, whenever the final position does not depend on $x$.

We have also derived three results that allow us to analyse a configuration without simulating every possibility:

  1. Identify a magic sequence: $(nt\bmod p^m)+n\leq p^m$.
  2. Identify a universally magic configuration: $n\mid p^m$.
  3. Count the magic sequences: $M=p^m-n+\gcd(n,p^m)$, for $n\leq p^m$.

The problem that I approached experimentally in the previous article, constructing tables and checking the various combinations on a computer, can thus be solved using simple properties of integer division and modular arithmetic.

References

  1. J. Harrison, T. Brennan, S. Gapinski, The Gergonne p-pile problem and the dynamics of the function x → (x + r)/p, Discrete Applied Mathematics 82 (1998), 103–113. ↩

  2. In November 2026, I submitted a formal request to Elsevier to correct the paper by Harrison, Brennan, and Gapinski (1998), reporting an error in the exponents in Lemma 3.1 and Theorem 3.2: the term $p^{m-i}$ should be replaced with $p^{i-1}$. The issue, which I had already brought to Prof. Harrison’s attention in 2017, was registered by Elsevier under reference no. 261107-010194. Documentation of the proposed correction is available here. ↩