The Friden algorithm may seem to be among the best kept secrets in Com-puter Science: that there exists a closed form, that is, noniterative, solution to the square root. You and I know, however, that we all learned a pencil and paper computation of the square root in high school — and promptly forgot it.
As fascinating and as valuable from a historical point of view as the Friden algorithm may be, it has little if any value when using other calcula-tors that lack the sticky, matrix-like keyboard of the Friden calculator. Nev-ertheless, it is the basis for the high school algorithm and gives us enough insight into the process that, with any luck, you will be able to remember it this time.
I’ll return to Equations [4.21] and [4.22] and set the modulus m to 10.
[4.25]
[4.26]
I’ll assume that I’ve already managed to find a by some method or other. I’m now looking for the next digit, b. Rewriting Equation [4.26] gives:
n = 10m+b
n2 = 100a2+20ab+b2
The High School Algorithm
79
or
[4.27] .
This formula gives a rule for finding b. Notice that the numerator of the right-hand side is the original number, less the square of a, shifted left so that the two line up. In short, the numerator is the remainder after I’ve sub-tracted the initial guess for the root. To get the denominator, I must double my guess a, shift it left one place, and add b.
At this point, if you’ve been paying attention, you’re asking how you can add b before you know what it is. The answer is that you can’t. This makes the square root algorithm a bit of a trial and error process, just like the divi-sion algorithm. In dividivi-sion, you also must guess at the quotient digit by looking at the first couple of digits of the dividend and divisor. You can’t be sure that this guess is correct, however, until you’ve multiplied it by the entire divisor and verified that you get a small, positive remainder.
In the same way, in the square root process, assume a value for b based on a divisor of 20a then substitute the new value of b and make sure the division still works.
It seems complicated, but it’s actually no more complicated than division, and in some ways it’s a bit easier.
When I first wrote Equation [4.26], I treated a and b as though they were single digits. However, you’ll note that there’s nothing in the math that requires that. I can now let a be all the digits of the root that I’ve found so far and let b be the one I’m seeking next. In division, the trial quotient digit must be tested and often adjusted at each step. The same is true in the square root algorithm. However, in the latter, the larger a is, the less influ-ence b will have on the square and the less likely you’ll have to backtrack and reduce a. Therefore, although you must still always check to be sure, there’s actually less backtracking in the square root. The only thing that makes the algorithm seem harder is that the roots, and therefore the differ-ences, are getting larger as you go along, as shown in Table 4.13.
As an example, I’ll use the same input value I used before: 1,234,567, 890. The first step is to point it off by twos.
12 34 56 78 90
Next, I write down the first digit of the root. I don’t need a fancy algorithm for this. The first piece of the argument is only two digits long, and its root can only be one of nine numbers. I should be able to figure out, by inspec-tion, which of the nine it is. In this case, four squared is 16, which is too large. So the first digit must be three. I write it down above the input value, square it, and subtract it to get the remainder.
As you can see, I’ve brought down the next digits, just as I would in divi-sion, except I bring them down by twos, so the next dividend is 334. This is the top half of the division in Equation [4.27].
The tricky part is remembering that the bottom half is not 10a + b, but 20a + b. Before I look for b, I must double the current root then tack on a zero (bet that’s the part you forgot). At this point I have
.
The division is 334/60, which yields five and some change. Before I write down the new digit, however, I must make sure that the division still works when I stick the five into the divisor. Now I have 334/65, which still yields a five. I’m OK, and I don’t need to backtrack. I write this next digit down and the trial root is now 35.
It’s very important for you to see that the trial root and the number you divide with are not the same because of the factor of two. The last
digit is the same, but the rest of the divisor is double that of the root.
3
12 34 56 78 90
9 3 34
3
12 34 56 78 90
9
60 3 34
The High School Algorithm
81
At this point, I have
.
What next? How do I subtract to get the new remainder? I can’t just square 35 and subtract it, because I’ve already subtracted the square of 3. Again, Equation [4.26] provides our answer. I have
, which I have already written in the form
.
I’ve also already performed the subtraction on the right; that’s how I got the remainder to divide with, to find b. Now that I’ve found it, I must com-plete the subtraction by subtracting out the left-hand side. That is, the remainder I now must obtain is
[4.28] .
In this example, b = 5, and 20a + b = 65, so I subtract 325. This step is eas-ier to do than to explain. Note that it’s identical to a division operation; that is, I multiply the last digit found by the “divisor” (65 in this example), and subtract it. After the subtraction, I have
.
At this point, you can begin to see how the algorithm works.
1. Point off the argument by twos.
2. Write down the first square root digit by inspection.
3. Subtract its square from the first two digits.
4. Obtain the remainder by drawing down the next two digits.
3 5
12 34 56 78 90
9
65 3 34
n2 = 100a2+20ab+b2
b 20a( +b) = n2–100a2
rem = n2–100a2–b 20a( +b)
3 5
12 34 56 78 90
9
65 3 34
3 25
9 56
5. Double the square root and append a zero.
6. Estimate the next root digit by dividing the remainder by this number.
7. Verify the digit by substituting it as the last digit of the divisor.
8. Multiply the last digit by the divisor.
9. Subtract to get the new remainder.
10. Repeat from step 5 until done.
By now, you should be able to complete the process, which looks like this:
Note again that the last digit of the divisor must always be the same as the multiplier when I subtract the next product. That’s because they’re both b.
Is the result correct? Yes. If you square 35,136, you get 1,234,538,496, which is smaller than the original argument. If you square the next larger number, 35,137, you get 1,234,608,969, which is too large. Thus the root I obtained, 35,136, is indeed the largest integer whose square is less than the original argument.
You’ve just seen the high school algorithm in all its glory. Now that you’ve seen it again, you probably recognize at least bits and pieces of it. If, like me, you’ve tried to apply it in later years and found that you couldn’t, it’s probably because you forgot the doubling step. The thing that you’re dividing to get the next digit (the divisor) is not the root, nor is it exactly twenty times the root. You get it by doubling the root, then appending a zero. Also, before finally multiplying this divisor by the new digit, don’t for-get to stick that new digit into the divisor in place of the original zero. When you do, there’s always the chance that the new product is too large. In that case, as in division, you must decrement the latest digit and try again.
3 5 1 3 6
12 34 56 78 90 initial argument 9 square first digit
65 3 34 draw down, divide by 20*root
3 25 subtract 5*65
701 9 56 draw down, divide by 20*root
7 01 subtract 1*701
7023 2 55 78 draw down, divide by 20*root 2 10 69 subtract 3*7023
70266 45 09 90 draw down, divide by 20*root 42 15 96 subtract 6*70266
2 93 94 remainder