Consider the following apparently simple problem. Letbe given by an approximation (for example a decimal or binary one). Decide if
is a rational number or not. Of course, this question as posed does not really make sense, since an approximation is usually itself a rational number. In practice however the question does make a lot of sense in many different contexts, and we can make it algorithmically more precise. For example, assume that one has an algorithm which allows us to compute
to as many decimal places as one likes (this is usually the case). Then, if one claims that
is (approximately) equal to a rational number
, this means that
should still be extremely close to
whatever the number of decimals asked for,
and
being fixed. This is still not completely rigorous, but it comes quite close to actual practice, so we will be content with this notion.
Now how does one findand
if
is indeed a rational number? The standard (and algorithmically excellent) answer is to compute the continued fraction expansion
of
. The number
is rational if and only if its continued fraction expansion is finite, i.e., if and only if one of the
is infinite. Since
is only given with the finite precision,
we be considered rational if
has a very large partial quotient
in its continued fraction expansion.
The following example illustrates Cohen's remarks:
? x %13 = 9495/3847 ? x*1.0 %14 = 2.4681570054587990642058747075643358461138549519105 ? contfrac(x) %15 = [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 2] ? contfrac(2.468157005458799064) %16 = [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 328210621945, 2, 1, 1, 1, 1, 7] ? contfracpnqn([2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1]) %17 = [9495 5852] [3847 2371] ? contfrac(2.4681570054587990642058747075643) %18 = [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 1885009518355562936415046, 1, 4] ? \p300 ? x*1.0 \\ notice that no repeat is immediately evident in the digits of x %19 = 2.468157005458799064205874707564335846113854951910579672472056147647517.. ? \\ in fact, the length of the period of the decimal expansion \\ of 1/3847 is 3846 (the order of 10 modulo 3847)!!