# Midy's theorem

In mathematics, Midy's theorem, named after French mathematician E. Midy, is a statement about the decimal expansion of fractions a/p where p is a prime and a/p has a repeating decimal expansion with an even period (sequence A028416 in the OEIS). If the period of the decimal representation of a/p is 2n, so that
    ${\frac {a}{p}}=0.{\overline {a_{1}a_{2}a_{3}\dots a_{n}a_{n+1}\dots a_{2n}}}$
then the digits in the second half of the repeating decimal period are the 9s complement of the corresponding digits in its first half. In other words,
    $a_{i}+a_{i+n}=9$
    $a_{1}\dots a_{n}+a_{n+1}\dots a_{2n}=10^{n}-1.$
For example,
    ${\frac {1}{13}}=0.{\overline {076923}}{\text{ and }}076+923=999.$
    ${\frac {1}{17}}=0.{\overline {0588235294117647}}{\text{ and }}05882352+94117647=99999999.$

## Extended Midy's theorem

If k is any divisor of h (where h is the number of digits of the period of the decimal expansion of a/p (where p is again a prime)), then Midy's theorem can be generalised as follows. The extended Midy's theorem states that if the repeating portion of the decimal expansion of a/p is divided into k-digit numbers, then their sum is a multiple of 10$^k$ − 1.
For example,
    ${\frac {1}{19}}=0.{\overline {052631578947368421}}$
has a period of 18. Dividing the repeating portion into 6-digit numbers and summing them gives
    $052631+578947+368421=999999.$
Similarly, dividing the repeating portion into 3-digit numbers and summing them gives
    $052+631+578+947+368+421=2997=3\times 999.$

## Midy's theorem in other bases

Midy's theorem and its extension do not depend on special properties of the decimal expansion, but work equally well in any base b, provided we replace 10$^k$ − 1 with b$^k$ − 1 and carry out addition in base b.
For example, in octal
    ${\begin{aligned}&{\frac {1}{19}}=0.{\overline {032745}}_{8}\\[8pt]&032_{8}+745_{8}=777_{8}\\[8pt]&03_{8}+27_{8}+45_{8}=77_{8}.\end{aligned}}$
In dozenal (using inverted two and three for ten and eleven, respectively)
    ${\begin{aligned}&{\frac {1}{19}}=0.{\overline {076{\mathcal {E}}45}}_{12}\\[8pt]&076_{12}+{\mathcal {E}}45_{12}={\mathcal {EEE}}_{12}\\[8pt]&07_{12}+6{\mathcal {E}}_{12}+45_{12}={\mathcal {EE}}_{12}\end{aligned}}$

## Proof of Midy's theorem

Short proofs of Midy's theorem can be given using results from group theory. However, it is also possible to prove Midy's theorem using elementary algebra and modular arithmetic:
Let p be a prime and a/p be a fraction between 0 and 1. Suppose the expansion of a/p in base b has a period of ℓ, so
    ${\begin{aligned}&{\frac {a}{p}}=[0.{\overline {a_{1}a_{2}\dots a_{\ell }}}]_{b}\\[6pt]&\Rightarrow {\frac {a}{p}}b^{\ell }=[a_{1}a_{2}\dots a_{\ell }.{\overline {a_{1}a_{2}\dots a_{\ell }}}]_{b}\\[6pt]&\Rightarrow {\frac {a}{p}}b^{\ell }=N+[0.{\overline {a_{1}a_{2}\dots a_{\ell }}}]_{b}=N+{\frac {a}{p}}\\[6pt]&\Rightarrow {\frac {a}{p}}={\frac {N}{b^{\ell }-1}}\end{aligned}}$
where N is the integer whose expansion in base b is the string a$_1$a$_2$...a$_ℓ$.
Note that b$^ℓ$ − 1 is a multiple of p because (b$^ℓ$ − 1)a/p is an integer. Also b$^n$−1 is not a multiple of p for any value of n less than ℓ, because otherwise the repeating period of a/p in base b would be less than ℓ.
Now suppose that ℓ = hk. Then b$^ℓ$ − 1 is a multiple of b$^k$ − 1. (To see this, substitute x for b$^k$; then b$^ℓ$ = x$^h$ and x − 1 is a factor of x$^h$ − 1. ) Say b$^ℓ$ − 1 = m(b$^k$ − 1), so
    ${\frac {a}{p}}={\frac {N}{m(b^{k}-1)}}.$
But b$^ℓ$ − 1 is a multiple of p; b$^k$ − 1 is not a multiple of p (because k is less than ℓ ); and p is a prime; so m must be a multiple of p and
    ${\frac {am}{p}}={\frac {N}{b^{k}-1}}$
is an integer. In other words,
    $N\equiv 0{\pmod {b^{k}-1}}.$
Now split the string a$_1$a$_2$...a$_ℓ$ into h equal parts of length k, and let these represent the integers N$_0$...N$_{h−1}$ in base b, so that
    ${\begin{aligned}N_{h-1}&=[a_{1}\dots a_{k}]_{b}\\N_{h-2}&=[a_{k+1}\dots a_{2k}]_{b}\\&\ \ \vdots \\N_{0}&=[a_{l-k+1}\dots a_{l}]_{b}\end{aligned}}$
To prove Midy's extended theorem in base b we must show that the sum of the h integers N$_i$ is a multiple of b$^k$ − 1.
Since b$^k$ is congruent to 1 modulo b$^k$ − 1, any power of b$^k$ will also be congruent to 1 modulo b$^k$ − 1. So
    $N=\sum _{i=0}^{h-1}N_{i}b^{ik}=\sum _{i=0}^{h-1}N_{i}(b^{k})^{i}$
    $\Rightarrow N\equiv \sum _{i=0}^{h-1}N_{i}{\pmod {b^{k}-1}}$
    $\Rightarrow \sum _{i=0}^{h-1}N_{i}\equiv 0{\pmod {b^{k}-1}}$
which proves Midy's extended theorem in base b.
To prove the original Midy's theorem, take the special case where h = 2. Note that N$_0$ and N$_1$ are both represented by strings of k digits in base b so both satisfy
    $0\leq N_{i}\leq b^{k}-1.$
N$_0$ and N$_1$ cannot both equal 0 (otherwise a/p = 0) and cannot both equal b$^k$ − 1 (otherwise a/p = 1), so
    $0<N_{0}+N_{1}<2(b^{k}-1)$
and since N$_0$ + N$_1$ is a multiple of b$^k$ − 1, it follows that
    $N_{0}+N_{1}=b^{k}-1.$

## Corollary

From the above,
    ${\frac {am}{p}}$ is an integer
Thus $m\equiv 0{\pmod {p}}$
And thus for $k={\frac {\ell }{2}}$
    $b^{\ell /2}+1\equiv 0{\pmod {p}}$
For $k={\frac {\ell }{3}}$ and is an integer
    $b^{2\ell /3}+b^{\ell /3}+1\equiv 0{\pmod {p}}$
and so on.