Period of Reciprocal of Prime

Theorem
Consider the decimal expansion of the reciprocal $\dfrac 1 p$ of a prime $p$.

If $p \nmid a$, the decimal expansion of $\dfrac 1 p$ is periodic in base $a$ and its period of recurrence is the order of $a$ modulo $p$.

If $p \divides a$, the decimal expansion of $\dfrac 1 p$ in base $a$ terminates.

Case $1$: $p \divides a$
Let $q = \dfrac a p$.

Then $\dfrac 1 p = \dfrac q a$.

So the decimal expansion of $\dfrac 1 p$ in base $a$ is $0.q$ and terminates.

Case $2$: $p \nmid a$
Let $d$ be the order of $a$ modulo $p$.

By definition, $d$ is the smallest integer such that:
 * $a^d \equiv 1 \pmod p$

By definition of modulo arithmetic:
 * $\exists x \in \Z: a^d - 1 = xp$

We can rearrange the terms to achieve the following expression:

so the decimal expansion of $\dfrac 1 p$ in base $a$ is $0.xxx...$ and is periodic of length $d$, which is the order of $a$ modulo $p$.