[Math] How to find out the number of repeating digits of a rational number in decimal form

rational numbers

Upon dividing two integers, I would like to programmatically predict the number of decimal places that repeat after the decimal point.

For example in $\frac{1}{3}=0.\overline{3}$, I want to know that the number of repeating digits is $1$.

In $\frac{89}{7}=12.\overline{714285}$, I want to get $6$.

Or in $\frac{1054}{13561}$, there are $665$ repeating digits.

I found these numbers in an empirical way; what I'd like now is a deterministic formula that would return this number:

$f(1,3) = 1$
$f(89,7) = 6$
$f(1054,13561) = 665$

Is there such a formula?


Edit: @ajotatxe made me find some interesting things with his comment:

If the divisor is a prime $p≠2,5$, the number of digits divides $p−1$.

First of all, I noticed that the numerator is totally irrelevant: whatever the numerator, the number of repeating decimals is always the same.

So let's focus on the denominator: if I take the prime $71$, there are 35 repeating digits. If I take the prime $191$, there are 95 repeating digits. So it does indeed divide $p-1$, but it's not always $(p-1)/2$ as I was hoping: for the prime $1571$, there are 1570 repeating digits.

Now back to my example $\frac{x}{13561}$ with 665 repeating digits.

Factored into primes, $13561 = 71 \times 191$.

Now let's multiply the number of repeating digits found for both primes: $35 \times 95 = 3325$. First thing to notice, this number divides $625$, which is the number I'm after.

And $3325/ 625 = 5$, which is the GCD of $35$ and $95$.

I can't figure out a deterministic way yet, but I feel like we're getting somewhere?

Best Answer

Your question is not well-defined because some fractions do not have a decimal expansion that consists only of repeating blocks after the decimal place, such as $\frac{1}{6}$. However, if the denominator $d$ is coprime to $10$, then what you are looking for is equivalent to the order of $10$ modulo $d$. In that case, we have reduced this to a previously solved question at Algorithms for finding the multiplicative order of an element in a group of integers mod m.