next up previous
Next: Pell's Equation Up: Lecture 20: Continued Fractions Previous: Lecture 20: Continued Fractions

Recognizing Rational Numbers

Suppose that you can compute approximations to a rational number using a computer, and desparately want to know what the rational number is. As Henri Cohen explains in his book A Course in Computational Algebraic Number Theory, continued fraction are very helpful.

Consider the following apparently simple problem. Let $ x\in\mathbb{R}$ be given by an approximation (for example a decimal or binary one). Decide if $ x$ 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 $ x$ to as many decimal places as one likes (this is usually the case). Then, if one claims that $ x$ is (approximately) equal to a rational number $ p/q$, this means that $ p/q$ should still be extremely close to $ x$ whatever the number of decimals asked for, $ p$ and $ q$ 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 find $ p$ and $ q$ if $ x$ is indeed a rational number? The standard (and algorithmically excellent) answer is to compute the continued fraction expansion $ [a_0, a_1, \ldots ]$ of $ x$. The number $ x$ is rational if and only if its continued fraction expansion is finite, i.e., if and only if one of the $ a_i$ is infinite. Since $ x$ is only given with the finite precision, $ x$ we be considered rational if $ x$ has a very large partial quotient $ a_i$ in its continued fraction expansion.

The following example illustrates Cohen's remarks:

Example 1.1  
? 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)!!


next up previous
Next: Pell's Equation Up: Lecture 20: Continued Fractions Previous: Lecture 20: Continued Fractions
William A Stein 2001-10-29