Table of Contents
 Number Systems and Representations
 Continued Fraction Representations
 Convergents of Continued Fractions
 Example: Continued Fraction Coefficients of \(\sqrt{14}\)
 Example: Convergents of \(\sqrt{14}\)
 Approximating Square Roots
Continued Fractions
Let's start part 2 of our discussion of computing square roots by talking about continued fractions. When we first learn mathematics, we learn to count in the base 10 system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. We can construct representations of all of the integers using these 10 digits, by arranging them in a different order. So, for example, saying 125 is equivalent to saying:
Later on in our mathematical lives, we learn that we can use other integers as our base, or radix, by expressing every integer as the sum of various powers of that integer. For example, 125 can be decomposed into powers of 2. In a base 2 system, we have only two symbols, 0 and 1, so 125 can be represented as 1111101, which is equivalent to saying:
Note that 1111101 is close to 1111111, which is \(2^7 = 128\).
As Knuth points out in his Art of Computer Programming, Part II:
"If our ancestors had invented arithmetic by counting with their two fists or their eight fingers, instead of their ten "digits," we would never have to worry about writing binarydecimal conversion routines. (And we would perhaps never have learned as much about number systems.)
This idea of an alternative radix to the traditional base 10 leads to entirely new number systems with their own interesting properties. However, it goes even further  as a high school student in 1955, Donald Knuth invented a number system with an imaginary, 4symbol radix in base \(2i\) (where \(i = \sqrt{1}\)), called the quaterimaginary base.
There are also number systems that use an irrational radix, such as phinary, which uses the golden ratio as its radix. Thus, the Golden Ratio \(\phi = \dfrac{1 + \sqrt{5}}{2}\) becomes \(\phi = 1\), \(2 = \phi^1 + \phi^2\) becomes \(2 = 10.01\), \(5 = \phi^3 + \phi^{1} + \phi^{4}\) becomes \(5 = 1000.1001\), and so on. In the case of the irrational number \(\sqrt{5}\), this number becomes a rational number in base \(\phi\): \(\sqrt{5} = \phi^2 + \phi^{1}\) becomes \(\sqrt{5} = 10.1\).
The single central idea behind these number systems is that the abstract set of integers \(\mathbb{Z}\) are being represented on an algebraic ring, which is a fundamental mathematical object. Initially the idea of a ring may seem strange, but it creates the foundations of modern number theory.
Continued Fraction Representations
(Note that while the next two sections will have a lot of "magic numbers," we will step through the procedure in the following sections, and it will be more clear where these numbers come from. There is also an excellent continued fractions calculator available online here.)
We can use other kinds of mathematical objects to represent different numbers. Another technique is to use continued fractions to represent numbers. These are wellstudied mathematical objects that date back to Euclid's Elements. The basic idea is to create a recursive expression that involves fractions nested beneath other fractions:
For ease of writing, this is often written as:
or, even shorter,
These variables \(a_i\) are called the terms of the continued fraction. Continued fractions can be used to represent rational numbers, in which case the continued fraction representation terminates after a finite number of terms. For example, \(\dfrac{125}{3} = [41; 1, 2]\),
Continued fractions can also be used to represent irrational numbers, in which case the continued fraction representation is a repeating pattern of variable length. For example, \(\sqrt{14} = [3; \overline{1,2,1,6}]\), where the line over the last digits indicates that the pattern repeats infinitely as \(1, 2, 1, 6, 1, 2, 1, 6, 1, 2, 1, 6, \dots\):
A few useful properties of these patterns, for square roots, are:

First, the integer portion (3 in the case above) is the largest integer whose square is less than the number (14).

Second, the sequence of integers that repeats is always palindromic, and it always begins repeating once it reaches a value of \(2 a_0\).
Here are a few more square roots represented as continued fractions, to help illustrate the above properties:
Next, we'll cover how to turn these terms \([a_0; a_1, a_2, \dots]\) into rational numbers \(\frac{P}{Q}\). These fractions are called convergents.
Convergents
We will show an example of how to compute a continued fraction in the next section, but we will cover one additional topic first. In order to be useful, we need some way to evaluate the continued fraction representation. One way to do this is to repeatedly compute common denominators, perform the fraction addition, and inert the result. The fraction that results is equivalent to the continued fraction expression, but is a rational number and therefore easier to evaluate.
It turns out that this rational number is a very important quantity called the convergent of \(\sqrt{n}\). However, it is cumbersome to perform the operation of derationalizing the continued fraction. There is a useful shortcut that takes the form of a recurrence relation.
The \(n^{th}\) convergent is defined as the rational number that results when the continued fraction representation is carried out to \(n\) terms, then expanded and simplified into a rational number. If a number like \(\sqrt{19}\) has an infinite continued fraction representation, it means we can compute progressively more accurate rational approximations.
For example, we know from the above that \(\sqrt{19} = [4; \overline{2,1,3,1,2,8}]\). Using this fact, we can use successive terms to write successive linear approximations:
If we continue this sequence, we get a slew of approximations (there are also additional approximations between each of these terms...):
The numerator and denominator of the \(k^{th}\) convergent are denoted \(P_k\) and \(Q_k\), respectively, and can be computed through the recurrence relation:
where the initial values are \(P_{2} = 0, P_{1} = 1\) for P and \(Q_{2} = 1, Q_{1} = 0\) for Q (making the first convergent equal to \(\frac{a_0}{1}\)). This can be used to compute successive approximations. Note that on a computational platform, you will quickly reach the end of your accuracy limit, so care must be taken for continued fraction sequences of longer than about 10 terms.
Example: Continued Fraction Coefficients of \(\sqrt{14}\)
Let's walk through an example of how to compute the continued fraction representation \([a_0; a_1, a_2, \dots, a_n]\) for a square root, and how to compute the \(k^{th}\) convergent.
Begin with the square root of a given number. For variety, we will use \(\sqrt{14}\). We begin by computing the nearest integer to 14's square root. If we don't have a square root routine, we can try integers by squaring them, and find the last integer that, when squared, is less than 14. This is \(a_0 = 3\). To interpret, \(a_0\) is the largest integer portion of our square root, with the residual portion \(\sqrt{14}3\) the portion that will be represented by the continued fraction.
Here's what we have:
We can drop the residual for an initial rational approximation of \(\sqrt{14} \approx 13\), but we can do better by going another step.
We recognize that the residual, which we want write in the form \(\dfrac{1}{\mbox{something}}\), can be written as \(\dfrac{1}{\dfrac{1}{\sqrt{14}3}}\):
Now examine the inverse residual term \(\dfrac{1}{\sqrt{14}3}\) for step 1, which we will call \(r_1\). Repeat the procedure we performed above: find the nearest integer portion to this quantity. In this case,
Now split this into its integer portion, which becomes \(a_1\), and its remaining fractional portion, .34833...:
and the new residual \(r\) is written in terms of the old residual and the coefficent \(a_i\):
Generalizing, we get an iterative procedure to determine the coefficients \(a_i\):
followed by:
where the initial values \(a_0, r_0\) are computed as mentioned above, and the rest of the values in the sequence follow.
Continuing for \(\sqrt{14}\), we get:
It is at this point that we see \(2 a_0\) and know that our (palindromic) sequence will repeat. (When we evaluate the convergents, we will utilize the palindromic nature of this sequence.)
Collecting these terms gives us the expected result: \(\sqrt{14} = [3; 1, 2, 1, 6]\).
This gives us an algorithmic procedure for computing the continued fraction representation \([a_0; a_1, a_2, a_n]\) of a number  with the important caveat, as mentioned above, that some integers have sequences that are extremely long before they repeat, making it impossible to find the full continued fraction representation without arbitrary precision libraries.
That being said, if you are only interested in the first 10 or so terms of the continued fraction representation, here is a static Java method to compute them:
/** Find the (shorter than 10) continued fraction sequence for sqrt(n).
* This returns a list of integers, [a0, a1, a2, ...]
*
* @params n Number to compute the square root of.
*/
public static List<Integer> continuedFractionSqrt(int n) {
if(isPerfectSquare(n)) {
throw new IllegalArgumentException("Error: n cannot be a perfect square.");
}
int niters = 10; // handbrake
int ai = 0;
double val = 0;
double remainder = 0;
List<Integer> coeffs = new ArrayList<Integer>();
// Fencepost
remainder = 1.0/Math.sqrt(n);
for(int i=0; i<niters; i++) {
val = 1.0/remainder;
ai = floor(val);
remainder = val  ai;
coeffs.add(ai);
if(coeffs.get(i) == 2*coeffs.get(0)) {
break;
}
}
return coeffs;
}
/** Check if x is a perfect square. */
public static boolean isPerfectSquare(int x) {
int s = (int)(Math.round(Math.sqrt(x)));
return x==(s*s);
}
/** Find the floor of a double. */
public static int floor(double j) {
return (int)(Math.floor(j));
}
Here is a short program that uses this routine to compute the continued fraction representation of \(\sqrt{14}\):
public class SquareRootCF {
public static void main(String[] args) {
System.out.println("sqrt(14) = "+Convergents.continuedFractionSqrt(14));
System.out.println("sqrt(19) = "+Convergents.continuedFractionSqrt(19));
}
}
and the corresponding output:
$ javac SquareRootCF.java && java SquareRootCF
sqrt(14) = [3, 1, 2, 1, 6]
sqrt(19) = [4, 2, 1, 3, 1, 2, 8]
Example: Convergents of \(\sqrt{14}\)
We now turn to the task of computing the convergents of the continued fraction, which will yield successive rational numbers that are progressively better approximations to \(\sqrt{14}\).
We start with the expression given above for the \(k^{th}\) convergent:
with initial values \(P_{2} = 0, P_{1} = 1\) for P and \(Q_{2} = 1, Q_{1} = 0\) for Q. This yields the first "real" convergent:
Successive approximations will use the values \(P_1\) and \(Q_1\) to compute the next convergents.
Continuing in this fashion gives:
and so on. This recurrence relation is easy to code up. It starts with the continued fraction coefficients for the given square root, and computes successive values of P and Q. The number of terms computed is specified by the user. Once it reaches the end of the sequence of continued fraction coefficients, it can start at the beginning again (the sequence is palindromic).
Finally, it returns the values of \(P_k\) and \(Q_k\), and of the successive convergents:
Here is a static method in Java that will compute the convergents of a square root:
/**
* Compute the convergents (rational representation of
* continued fraction).
*
* This uses the recurrence relation:
*
* P_n P_n2 + a_n P_n1
*  = 
* Q_n Q_n2 + a_n Q_n1
*/
public static long[] convergents(int n, int nterms) {
long[] convergents = new long[2];
List<Integer> cfrepr = continuedFractionSqrt(n);
// Initial values for convergent recurrence relation
long Pnm2 = 0; // P_{n2}
long Pnm1 = 1;
long Qnm2 = 1;
long Qnm1 = 0;
long P = 0;
long Q = 0;
// Term 0 is the constant value a0.
int accessindex = 0;
for(int i=0; i<=nterms; i++) {
int an = cfrepr.get(accessindex);
P = Pnm2 + an * Pnm1;
Q = Qnm2 + an * Qnm1;
Pnm2 = Pnm1;
Pnm1 = P;
Qnm2 = Qnm1;
Qnm1 = Q;
if(accessindex+1>=cfrepr.size()) {
// Ensure we keep repeating the sequence
// if the sequence has fewer terms than
// the user asks for.
// This allows us to get really good
// approximations for numbers.
// This only works because the sequence
// is palindromic.
accessindex = 1;
} else {
accessindex++;
}
}
convergents[0] = P;
convergents[1] = Q;
return convergents;
}
Here is a simple driver program that prints out several convergents for \(\sqrt{14}\):
public class SquareRootCF {
public static void main(String[] args) {
for(int i=1; i<=10; i++) {
long[] conv = Convergents.convergents(14,i);
System.out.println("Convergent "+i+": "+conv[0]+"/"+conv[1]);
}
}
}
and the corresponding console output:
$ javac SquareRootCF.java && java SquareRootCF
Convergent 1: 4/1
Convergent 2: 11/3
Convergent 3: 15/4
Convergent 4: 101/27
Convergent 5: 116/31
Convergent 6: 333/89
Convergent 7: 449/120
Convergent 8: 3027/809
Convergent 9: 3476/929
Convergent 10: 9979/2667
References

Hardy, G. H. A Course of Pure Mathematics. Cambridge University Press, Tenth Edition (19081967).

Knuth, Donald. The Art of Computer Programming, Volume 2: Seminumerical Algorithms. AddisonWesley Publishing Company, Second Edition (1975).