Let *p* be a prime. If the repeating decimal for the fraction *a*/*p* has even period, the the second half of the decimals are the 9’s complement of the first half. This is known as Midy’s theorem.

For a small example, take

1/7 = 0.142857142857…

and notice that 142 + 857 = 999. That is, 8, 5, and 7 are the nine’s complements of 1, 4, and 2 respectively.

For a larger example, we can use Mathematica to look at the decimal expansion of 6/47:

```
In: N[6/47, 60]
Out: 0.127659574468085106382978723404255319148936170212765957446809
```

and we can confirm

12765957446808510638297 + 87234042553191489361702 = 99999999999999999999999

Let’s do another example with 6/43:

```
In: N[6/43, 50]
Out: 0.13953488372093023255813953488372093023255813953488
```

Does the theorem hold here? The the hypothesis of the theorem doesn’t hold because the fraction has period 21, which is not even. Can the conclusion of the theorem hold anyway? No, because we can’t split 21 digits in half! But we can split 21 digits into three parts, and if we do we find

1395348 + 8372093 + 232558 = 9999999

Let’s finish up with an example in another base. We’ll look at the fraction 3/7 in base 17.

In: BaseForm[N[3/7, 24], 17] Out: 0.74e9c274e9c274e9c27_{17}

If we split the repeating part of the decimal into two parts we get 74e and 9c2, and these add up to the base 17 analog of all 9’s.

In: BaseForm[17^^74e + 17^^9c2, 17] Out: ggg_{17}

That is, the base 17 numbers 9, c, and 2, are the g’s complements of 7, 4, and e respectively.

[The Mathematica operator `^^`

interprets its right argument as a number whose base is given by the left argument. The `BaseForm`

function takes a number as its first argument and returns its representation in the base given as its second argument.]

**Related post**: Casting out Z’s

Wikipedia provides a surprisingly simple proof: https://en.wikipedia.org/wiki/Midy%27s_theorem