Decimal Recurring Cycles and Cyclic Groups
From the Perspective of Cyclic Groups
When the Denominator and 10 are Coprime
The process of division is actually the calculation of , which is the remainder of the k-th division. When the remainder is 1, i.e., , it indicates a cycle. However, this may not be the first cycle, as the remainder might have appeared twice at another number, such as 2, hence the cycle would be shorter.
First, consider when 10 and n are coprime, forming a cyclic group , the order of the cyclic group is the smallest number that makes it remainder 1, and it must be a factor of . Obviously, if is prime, then the order is .
This is because the group is closed, the product of any two group elements is still an element of the group. If we consider all possible powers of , this set is also closed under modulo m multiplication. Then, if generates a cyclic group, the order of the group (the number of elements in the group) is the smallest positive integer , such that . This order must divide any for which holds, including .
Since always holds (according to Euler’s theorem), it means that the order of the group must be a factor of . Because if there exists some such that , then must be a multiple of to ensure the group’s cyclic nature and the definition of order.
In short, the cycle of powers completes at most once upon reaching , but the actual cycle may complete at an earlier point, thus the actual cycle length (the order of the group) is a factor of .
Calculating the order Besides directly starting from 1 and finding the smallest positive integer making , there are simplifications, especially when k is very large.
When , where and are different primes, and is coprime with both and .
Suppose the order of the multiplicative group of is , and the order of the multiplicative group of is . According to Euler’s theorem, we know and , so and .
The order of the multiplicative group of , denoted as , is the smallest positive integer such that . Since , according to the Chinese Remainder Theorem, if we can simultaneously satisfy and , then also holds.
To find , we need to find the smallest such that and . This means must be a multiple of both and . Therefore, .
This leads to another question, how to quickly calculate the order of ? We only know the order must be a factor of .
When the Denominator and 10 are Not Coprime
When 10 and n are not coprime, n’s factors include 2 or 5, let , m is coprime with 10, a,b are natural numbers and not all 0. In this case,
When k is large enough, it must be 0. We only need to prove that multiplying a finite decimal by an infinite recurring decimal does not change the length of the recurring cycle. This needs proof, but I don’t know how.
That is, we don’t need to consider the factors of 2 or 5, just extract the factors that are coprime with 10. (I still don’t know how to prove this).
Algorithm
First, remove 2 and 5 completely, the value must be a combination structure like (some exponents can be 0), at this time is less than , then calculate the least common multiple, definitely less than . That is to say, for the prime p, its order is necessarily greater than the order of any number smaller than it. So just find the largest prime within 1000, and compare it with the number that has been removed up to 1000 and is larger than .
This involves the question, between two adjacent primes p, q, is it possible for a number divided by 2 to be larger than p? This is actually equivalent to whether there exists such that . I don’t know, but it’s impossible within 1000.
So, between two adjacent primes p,q, is there not a multiple of 2 or 5, not a prime, but the order of 10^k mod x is larger than the order of 10^k mod p? That is,
Could it be larger than ?
This number must be less than the product of these Euler’s functions
And . Moreover, except
for the prime number 2, p-1 is necessarily an even number, so there must be a common factor of 2, back to the previous question, at least within 1000 it is impossible. After dividing by 2, it must be smaller than .
Therefore, the number with the longest cycle must be a prime number. If it can be proved:
- Between two adjacent prime numbers p, q, any number divided by 2 is definitely smaller than p.
- Multiplying a finite decimal by an infinite recurring decimal does not change the length of the recurring cycle.
It seems that there are related theorems that can prove the second point, and also obtain the length of the non-recurring part.
Conventional Intuitive Approach
A recurring decimal must be infinite, and the digits in the recurring cycle may repeat. Based on coding experience, without reading to the last digit, it is impossible to know whether this string of digits is a recurring cycle. Therefore, a maximum possible recurring cycle length must be set.
Secondly, even if it is a recurring cycle, it may not be the shortest recurring cycle; it could be a multiple of the shortest recurring cycle. For example, the cycle of 001 also satisfies the cycle of 001001. So when a cycle is found, its factor length cannot be the cycle length.
The recurring part, choosing the length of the recurring cycle from anywhere, its period does not change. Like the 001 cycle, in fact, only looking at the latter part can also be considered as a 010 cycle.
So, as long as you skip enough decimal places, it will definitely be the recurring part, and the period remains the same. Just look for two segments that match up according to the period length.
1 | -- Generates the decimal representation of 1/deno after the decimal point. |
Considering the Length Characteristics of the Recurring Cycle
The recurring part of a recurring decimal is actually a number multiplied by a geometric series, which can be expressed as
According to the sum formula, we can get
It’s observed that the length of the recurring cycle n is related to, or determined by, the denominator. As long as the denominator can be transformed into the form , it will definitely be a recurring decimal. That is to say, for the fraction , find a positive integer such that , then this is the length of the recurring cycle.
The improved code to avoid integer overflow is as follows:
1 | -- Improved version of recurCycle that utilizes an infinite list of numbers composed entirely of 9s to find the cycle length. |