3 ms·
The length of the repeated part is the multiplicative order of 10 mod q [1]. Look at the Wikipedia page on repeated decimals and you can see many of the repeat
by abetusk 9y ago
The length of the repeated part is the multiplicative order of 10 mod q [1].
Look at the Wikipedia page on repeated decimals and you can see many of the repeated sequences have length (q-1) ((q-1) is phi(q) when q is prime) [2].
There's a SO answer that talks about this [3]. I think I can derive it here, though take it with a grain of salt as I might have screwed something up.
Say you have a rational number p / q, and w.l.o.g., p < q. Consider an integer, R, that corresponds to the repeated portion of the rational number (in base 10), with length s (in decimal). This implies:
p/q = R sum_{k=1}^{\infty} 10^{-s k} = R / ( 10^s - 1 )
rearranging terms:
p (10^s - 1) - q R = 0
Let's just assume for now 10 and q are relatively prime. I think I have to do a little hand waiving at this point and just assume the above has a solution. If the above is true, this means 10^s = 1 (%q) for s equal to the order of 10 mod q, which is a fancy way of saying 10^s = C q + 1 for some integer C. One choice that will always work is taking s = phi(q) though this might be smaller (specifically a divisor or phi(q)) depending on what the order of 10 is to q.
So to make the above equation true, let's be coarse and choose s = phi(q) in the below:
.. p (10^{ phi(q) } - 1 ) - q R = 0
-> p ( C q ) - q R = 0
-> q ( p C - R ) = 0
And there you go. Assuming that I haven't bungled anything, this also gives an 'algorithm' to find the actual repeated sequence: R = p C = p 10^{phi(q)} . Choosing s, the length of R in base 10, to be phi(q) will always "work", but, again, s might be able to be chosen smaller, depending on what divisors of phi(q) there are and what order 10 is to q, so the real answer of the length of the repeated sequence is 10's order in the multiplicative group mod q.
There's also the case of when 10 and q aren't relatively prime that needs to be worked out.
You can also see how this argument could be modified to work with other bases.
[1] https://softwareengineering.stackexchange.com/a/192077 https://softwareengineering.stackexchange.com/a/192077
[2] https://en.wikipedia.org/wiki/Repeating_decimal#Decimal_expansion_and_recurrence_sequence https://en.wikipedia.org/wiki/Repeating_decimal#Decimal_expa...
[3] https://softwareengineering.stackexchange.com/a/192081 https://softwareengineering.stackexchange.com/a/192081
- danbruc 9y agoThis made me look at multiplicative order again and I finally found [1] what I could not find at the time, someone explicitly stating that this is a hard problem. Now it seems so obvious that I should have looked at what cryptography has to say about it that I can not imagine why I did not. Anyway, case finally closed, I got my peace of mind now. [1] https://math.stackexchange.com/questions/837489/algorithms-for-finding-the-multiplicative-order-of-an-element-in-a-group-of-inte https://math.stackexchange.com/questions/837489/algorithms-f...