Write out the decimal number obtained by dividing two given integers A and B. The number might be a recurring decimal, e.g. 1 / 3 = 0.(3)
Rational numbers can be written as ratio of two integers while irrational numbers like \( \pi \) cannot.
In order to format the decimal number into a string one could write code to perform a simple long division and checking for the maximum repeating substring in its decimal digits. A smarter approach though would be to notice that while doing decimal long divisions between the remainder and the divisor, the operation is similar to a state machine: if \( n = \frac{a}{b} \), its recurring decimal cycle (if present at all) can be at most \( b-1 \) long. This follows logically from the definition of long division.
Therefore the simplest algorithm to identify such cycles would be to keep an hash table of the remainders calculated and identify a cycle when a remainder has been already encountered
Calculating period’s digits
We might be interested at times into precalculating the number of digits that come before the period or that form the period itself. Number theory distinguishes between three kind of rational numbers:
Regular numbers whose decimal expansion is finite (e.g. \( 1/2 = 0.5 \)). Let \( r \equiv p/q \), after factoring common multiples it follows that these numbers are of the form
\[r=\frac{p}{2^\alpha5^\beta} \quad p \not\equiv 0 \pmod {2,5}\]
Nonregular numbers whose decimal expansion is formed by a recurring period only. These are formed by having prime factors coprime to 10
Nonregular numbers with non-cycling digits before the period (also called antiperiod) and then having a recurring period. These have both coprime factors and factors of the form in the first bullet point.
Therefore to know the length of the period one should first check if the denominator has 10-coprime factors. If such factors are present the length of the period is given by the multiplicative order of the denominator, i.e. to the maximum \( k \) given by the discrete logarithm
\[10^k \equiv 1 \pmod f\]
where \( f \) is a 10-coprime factor and \( k \) is the minimum positive integer value for which the equivalence is verified.
The length of the antiperiod is given by the maximum power factor between 2 and 5 factors of the denominator after factoring common ones out with the numerator.
The following code formats a rational number given in the same form as before by applying the concepts explained above and using a trial-factoring algorithm together with a simple discrete logarithm implementation and a fast (but quite limited due to the huge numbers that might be involved with bigger inputs) exponentiation by squaring
It has to be noted that the formatting problem isn’t particularly suited to be computed with this latter period-estimate method since discrete logarithm is a notoriously hard problem so the second approach would easily be outperformed by the first one given huge numbers. Anyway the code above might be useful to practically show how to approach problems requiring calculating the size of a rational number (anti)period.