# Why does this algorithm work (where does it come from) for finding the period of a decimal expansion?

For the period of something like $1/d$ where $d$ is a positive integer, I saw an algorithm repeatedly doing:

\begin{align*}r &= 1\\ r &= 10r \bmod d \quad\text{ (until } r = 1) \end{align*}

and the number of steps was the period.

-
The period is the smallest positive $k$ such that $10^k\equiv 1\pmod{d}$. Of course we must put restrictions on $d$. Assume that neither $2$ nor $5$ divides $d$. – André Nicolas May 26 '12 at 22:17
It does not work for $d=6$ – Henry May 26 '12 at 22:20
Ah yes I had it wrong but it was just long division. – Palace Chan May 27 '12 at 1:00

$$2/5 = (2.0)/5 = 0.4$$
So in this case since we are doing $1/d$ we start with one. How many times this process is repeated is just how many times we carried the remainder over which is the length of the decimal.