4

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?

BenMorel
  • 203
  • I wouldn't necessarily say that you found them through empirical methods. – marshal craft Jul 04 '15 at 10:20
  • 1
    If the divisor is a prime $p\neq2,5$, the number of digits divides $p-1$. – ajotatxe Jul 04 '15 at 10:21
  • @marshalcraft I found them using a big number library, dividing to a scale of 1000 decimals, then finding where the string repeats. I wouldn't say this is a very scientific approach. – BenMorel Jul 04 '15 at 10:23
  • @ajotatxe That's a very interesting start, is there more information we can get to find the exact number of digits? – BenMorel Jul 04 '15 at 10:24
  • I'd say that further information is hard to obtain, if possible. It has to do with discrete logarithms, that, as far as I know, aren't easy to compute efficiently. – ajotatxe Jul 04 '15 at 10:26
  • One method I found is that find the smallest number 9999... n digits that is multiple of the denominator. n is the required no. of repeating digits. But I dont know how can it be used in a program for big n. – Bhaskar Jul 04 '15 at 10:35
  • 1
    You could use long division to compute the decimal part. For $\frac{a}{b}$ compute $(\frac{r^{10}}{b})^{-10}$ where $r = a$ $ mod$ $b$. The alogrithm can keep appending decimal points but it essentially repeats this process. Sorry if there is a better way. – marshal craft Jul 04 '15 at 10:39
  • There's a general algorithm described here: https://en.wikipedia.org/wiki/Repeating_decimal#Fractions_with_prime_denominators The algorithm is slightly simpler when the fraction is already reduced, that is, when $a, b$ in $f(a, b)$ are coprime; you can find a worked example of the form $f(1, b)$ here: http://math.stackexchange.com/a/1267645/155629 . – Travis Willse Jul 04 '15 at 10:49
  • @ajotatxe I edited the question with some more insights, maybe you could have a look! – BenMorel Jul 04 '15 at 11:11
  • @Travis Thanks for the pointer, I missed this article on Wikipedia! That's really interesting, though I can't figure a generic formula out of it, if there's any? – BenMorel Jul 04 '15 at 11:12
  • @Benjamin One could write down a generic formula, but that would probably be much less enlightening than thinking about this as a general algorithm. One of the sticking points is that powers of $2$ and $5$ in $b$ behave differently than other prime factors, on the account that those are the prime factors of our usual number base, $10$. – Travis Willse Jul 04 '15 at 11:14
  • @Benjamin If you're satisfied with the algorithm, I encourage you to work out the example you talked about and then post an answer in reply to your question here. – Travis Willse Jul 04 '15 at 11:16
  • @Travis There's no problem if the algorithm has if branches depending on the powers of $2$ and $5$ in $b$, but so far I have still not been able to put everything together and get a working algo. – BenMorel Jul 04 '15 at 11:18
  • 1
    The period of the decimal representation of $\frac1n$ is OEIS A007732. There is no nice formula, but the entry does offer a MAPLE algorithm. – Brian M. Scott Jul 04 '15 at 20:00

1 Answers1

3

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.

user21820
  • 57,693
  • 9
  • 98
  • 256
  • You make a good point about $\frac{1}{6}$! – BenMorel Jul 04 '15 at 11:08
  • He has said the answer basically you are looking for smallest integer $k$ such that $10^k=1$ $mod$ $d$. It uses congruence mods hopefully you are familiar with, if not look it up I recommend. If $d$ is prime then it has to be a factor of $d-1$. Unfortunately you will have to at this point simply try the factors. There is no known formula to tell order of a given number mod $p$ for all numbers. – marshal craft Jul 04 '15 at 11:42
  • @marshalcraft: Smallest positive integer $k$, yes. In general the order divides $φ(d)$. If $d$ is prime, $φ(d) = d - 1$. – user21820 Jul 04 '15 at 12:41