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?
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.