# Finding minimal period of rational

I'm trying to find the decimal representation minimal period of $1/n$ where $n$ is an integer.

I'll clarify colloquially because I'm very noob with math terms:

$$1/3 = 0,(3)$$ $$DP(3) = 1$$ $$1/7 = 0.(142857)$$ $$DP(7) = 6$$

After a long search on Wikipedia found that the period of $1/p$ where $p$ is prime equals to the multiplicative order of $10$ modulo $p$:

decimal_period = ->(p) do
pwr = 1
pwr += 1 until ((10**pwr).modulo(p)) == 1
pwr
end


This works for primes but I failed to find an algorithm for all other integers and I'm stuck here, any suggestion?

-
en.wikipedia.org/wiki/Repeating_decimal appears to give a lot of information on this. See the subsection Reciprocals of composite integers coprime to 10.'' There are many different cases to consider... – Lord Soth Apr 12 '13 at 20:55
A non-prime number can't have a period bigger than $1$ right? If so this question is stupid :/... – nicooga Apr 12 '13 at 21:03

You can ignore the primes $2$ and $5$. The greater exponent of $2$ and $5$ is the number of digits before the repeat starts. For example, $\frac 16=0.1(6)$. The leading $1$ is caused by the $2$ that is a factor of $6$. Similarly, $\frac 1{12}=0.08(3)$ has two digits before the repeat as $2^2$ divides into $12$. The period of a prime $p$ will always be a factor of $p-1$.
The multiplicative order of $10$ works fine for non-primes just as well. You can also take the least common multiple of the periods of the primes that multiply into a composite. For example, $\frac 1{41}=0.(02439)$, with a period of $5$. Then $\frac 1{7*41}=\frac 1{287}$ has period $5*6=30$. And it does.