12

If you have a fraction such as $$\frac{7}{26}=0.269230\overline{769230}$$ where there are a number of digits prior to the repeating section, how can you tell how many digits there will be given just the fraction?

I believe I could run through the standard long division algorithm until I come across the same remainder for the second time and then use the location of the first instance of this remainder to calculate the number of digits before the repeating section, but this feels very cumbersome.

After lots of reading online, I came across what looks like a formula for it from Wolfram MathWorld:

When a rational number $\frac{m}{n}$ with $(m,n)=1$ is expanded, the period begins after ${s}$ terms and has length ${t}$, where ${s}$ and ${t}$ are the smallest numbers satisfying $10^s\equiv10^{s+t}\pmod{n}$.

I know how to calculate the length of the period of a fraction, and so in the case of my original fraction we have $10^s\equiv10^{s+6}\pmod{26}$, but I don't know how to solve for ${s}$ in this equation!

So there are really two questions here - the one in the title, and a sneaky one about how to take logs in a modulo arithmetic equation.

  • 1
    Nobody else knows how either; it's an open problem. You can use trial-and-error, and there are some things you can do with Fermat's theorem to cut down the number of exponents you have to check, but I don't think anything significantly better than brute force is known. – MJD Mar 26 '14 at 00:37
  • @MJD That's not quite true; the algorithm is quite simple, but there isn't a constant-time closed form AFAIK. – Alex Becker Mar 26 '14 at 00:38
  • I would think it has something to do with it's continued fraction length. Can anyone confirm this? – Bobby Ocean Mar 26 '14 at 00:38
  • I will cheerfully disconfirm that. – MJD Mar 26 '14 at 00:39
  • Do you have a counter example? Not really a number theorist, but I remember reading something about how each partial continued fraction represents the best rational approximation of the continued fraction. Ergo, partial continued fractions tend to look more and more irrational. – Bobby Ocean Mar 26 '14 at 00:42
  • 2
    @BobbyOcean You are correct that they are the best rational approximation, but that's very different than the best decimal approximation. – Alex Becker Mar 26 '14 at 00:44
  • 5
    I disagree with the point you chose to claim as beginning repetition in $7/26$. It can be considered as repeating with the second decimal place. – hardmath Mar 26 '14 at 00:45
  • @Becker First I don't know if my statement is true, hence I would like a counter example. Evenmoreso, we are discussing not just decimal approximation, but repeating decimal approximation which is in essence a property concerning rational numbers. – Bobby Ocean Mar 26 '14 at 00:48
  • Also, just to point out, the title is a little funny since all fractions eventually start to repeat. – Bobby Ocean Mar 26 '14 at 00:50
  • @hardmath True, and indeed $22 \equiv 10^2 \equiv 10^8 \mod 26$ – fgp Mar 26 '14 at 01:08
  • 1
    @BobbyOcean Note that the continued fraction representations for the numbers $\frac1n$ all have the same length; each is $[0; n]$. But they have completely different base-10 periods, for example the period of $[0; 3]$ is 1 and the period of $[0; 17]$ is 16 and the period of $[0; 21]$ is 9. – MJD Mar 26 '14 at 01:15
  • @MJD Thanks. :-) However, I thought we were interested in the length s that comes before the repetition. Meaning that 1/n each have the same continued fraction length, but they also have the same s=0, where s is the portion before the repetition begins. – Bobby Ocean Mar 26 '14 at 01:21
  • @hardmath, you're right, my mistake. I'll calculate the length of the pre-period by tracking remainders during a long division, I'm not concerned about efficiency here. – Sandy MacPherson Mar 26 '14 at 01:32
  • @MJD It is the length s that I'm after here as I can already calculate the length of the period. When you said "It's an open problem" I presume you are referring to the one of solving the modulus equation above? If so, is there another formula for calculating s? – Sandy MacPherson Mar 26 '14 at 01:34
  • Bearing in mind @hardmath's correction, I've noticed a pattern that seems to work. For terminating decimals, I believe that if you break down the denominator into prime factors, the higher of the number of 2s or 5s in the factors tells you the number of decimal places that the fraction will expand to. But it looks like with repeating decimals, this count indicates the value of s. For example, 26 = 213 so s=1, 84 = 2237 so s=2, 6 = 2*3 so s=1 etc. Is this just co-incidence or is this a valid approach? – Sandy MacPherson Mar 26 '14 at 01:45
  • @BobbyOcean Note that the continued fraction representations for the numbers $\frac1{10^n}$ all have the same length; each is $[0; 10^n]$. But they have completely different values of $s$, namely $s=n-1$. – MJD Mar 26 '14 at 16:38

3 Answers3

7

Rewrite the fraction as $$\frac{m}{n}=\frac{p}{10^sq}$$ where $p,q$ are coprime and $q$ is not divisible by $2$ or $5$ while $p$ is not divisible by $10$. Computing $s$ (the pre-period) is easy; it is the larger of the number of times $2$ divides $n$ and the number of times $5$ divides $n$. Then we want the smallest $t$ such that $10^t\equiv 1\;(\bmod\;q)$. By Fermat's little theorem, we have $10^{\varphi(q)}\equiv 1\;(\bmod\;q)$, thus $\;t|\varphi(q)$ so it suffices to check the divisors of $\varphi(q)$.

Alex Becker
  • 60,569
  • 1
    It would be $\varphi(q)$, not generally $q-1$. And if I read the question right, the OP is more interested in the length of the pre-period than of the period, that would be $s$, provided $10 \nmid p$. – Daniel Fischer Mar 26 '14 at 00:49
  • This is similar to what I have done to calculate the length of the period but, as @DanielFischer said, it's the length of the pre-period I am interested in. Ultimately I am trying to custom-render a repeating decimal and work out how many digits I need in total and where to draw the overbar, all given a simple fraction. – Sandy MacPherson Mar 26 '14 at 01:25
  • @DanielFischer Thanks, I tricked myself with my own notation. – Alex Becker Mar 26 '14 at 01:46
  • @AlexBecker you seem to have confirmed and formalised the pattern I noticed (see my comment on the original post). Thanks, I'll mark this as the correct answer. – Sandy MacPherson Mar 26 '14 at 01:50
1

In the $2^{nd}$ section of this answer we get the answer using the high-school algorithm that the OP mentioned, but eschewed as being too cumbersome. But by studying the theory behind the algorithm and using elementary number theory, we will be able to understand, directly, the wolfram theory on decimal expansions at formulas/examples (7) - (9).

For the zero divisor $[10] \in {\textstyle \mathbb {Z} /26\mathbb {Z}}$, calculations show that

$\quad x \pmod{26} \text{ where } x \in [10^0, 10^1, 10^2, 10^3, 10^4, 10^5, 10^6, 10^7] = [1, 10, 22, 12, 16, 4, 14, 10]$

The numerator $7$ of the fraction $\large \frac{7}{26}$ carries over to a unit, $[7] \in {\textstyle \mathbb {Z} /26\mathbb {Z}}$.

So the unit $[7]$ 'goes for a ride' over the 'exponential graph' of zero-divisors generated by $[10]$,

$\quad [1\cdot7,10\cdot7,\, 22\cdot7,\, 12\cdot7,\, 16\cdot7,\, 4\cdot7,\, 14\cdot7]\quad \text{(not necessary to to calculate any of these residues)}$

Conclusion: In the decimal expansion of $\large \frac{7}{26}$, the repeating block of digits is of length $6$ and begins at the $2^{nd}$ fractional decimal digit ($10^7 \equiv 10^1 \pmod{26}$).

With this behind us, we can easily get the explicit answer - multiply the numerator by $10^7$ and keep the 7 quotient digits (padding $0s$ after the decimal point might be necessary) after dividing,

$\quad 7 \cdot 10^7 = 26\cdot2692307+18$

and (we've got the seven digits in the quotient),

$\quad \large \frac{7}{26} \approx 0.2\overline{692307}$

The same pattern (1 plus 6 block) occurs whenever the numerator of $\large \frac{n}{26}$ satisfies

$\quad 1 \le n \lt 26 \land n \text{ is odd } \land n \ne 13$


Analyzing the high-school algorithm (see some theory here), there are at most $26$ divisions (neophyte estimate) that have to be performed. So let us just jump into it!

Expand $\large \frac{7}{26}$:

Divide Approximate (append $q$ digit)
$7\cdot 10 = 26 \cdot 2 + 18$ $\large \frac{7}{26} \approx 0.2$
$18\cdot 10 = 26 \cdot 6 + 24$ $\large \frac{7}{26} \approx 0.26$
$24\cdot 10 = 26 \cdot 9 + 6$ $\large \frac{7}{26} \approx 0.269$
$6\cdot 10 = 26 \cdot 2 + 8$ $\large \frac{7}{26} \approx 0.2692$
$8\cdot 10 = 26 \cdot 3 + 2$ $\large \frac{7}{26} \approx0.26923$
$2\cdot 10 = 26 \cdot 0 + 20$ $\large \frac{7}{26} \approx 0.269230$
$20\cdot 10 = 26 \cdot 7 + 18$ $\large \frac{7}{26} \approx 0.2692307$

Now the residue $18$ has already appeared and was used after calculating the first fractional decimal; the answer is summarized as follows:

$\quad \large \frac{7}{26} \approx 0.2\overline{692307}$

CopyPasteIt
  • 11,366
1

'Cumbersome' is what computers are meant for! Here is an algorithm I converted into a python program to do something similar for Project Euler 26.