HOWTO-square-root (2001/08/15) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I thought it was about time to publish a nifty, little algorithm for computing the square-root of a number. Here it is:
Do you somehow doubt this algorithm actually works and do you think the lines above are a hoax? Ok, then it's time for another proof that you can verify with your calculator ;-)
The square-root of 1234 is approx. 35,128.... Is that accurate enough for a quick guess? You can run this algorithm without a calculator, at least to some degree, even if you are a math illiterate (like me ;-) Now for the best part of it all: This algo works just as well if implemented for base 16. Instead of two digits you take two nibbles (also known as a byte) and instead of multiplying by 10 you multiply by 16 (or left shift four times). This does sound like you could implement it pretty damn fast in any assembly language, doesn't it? No multiplication, only simple subtraction loops... I wanted to contribute this algorithm to the GnuMP project, but I have yet to find the time and devotion to dig into the GMP number format to implement it correctly. And I would also have to think about when to stop the calculation of irrational square-roots. The algorithm lets you continue until infinity, if you like, but you probably don't have that much time :-)
Continued...I thought a little bit longer about the algorithm and found a way to compute the cubic root of a number in a similiar manner. Take a look at the tables below. It seems you can find a cubic root by subtracting the sequence of step * 3rd odd numbers (1, 7, 19, 37 etc.) from each triplet of digits? But I don't (yet) know what to do with the remainders, ie. what sum do I have to build to begin the subtraction with in the next step:
12*12*12 = 1728; remainder 728 which is 331 + (331+66) Now what is so special about the magic number 331? And where do the additional summands for the following values come from? What's the origin of this number magic and why is the additional distance in every new term 66 + 6 * (step-1)? I don't see the background for this.. only the facts. It looks like I have to go back to the drawing board (or the keyboard for that matter) and play around with the numbers to see if and how a cubic root can really be found with an algorithm similiar to the one for the square root. Table of square, cubic and "to the power of 4" numbers
There is an order in the 3rd column of distances too, do you see it? Look at the distances between the distances of the n-th odd numbers... 7-1=6, 25-7=18 (6+12), 55-25=30 (6+12+12), 97-55=42 (6+12+12+12), 151-97=54 (6+12+12+12+12), 217-151=66 (6+5*12) etc. I wonder how this could be implemented in an algorithm... and I also wonder if one could generalize this approach to computing roots of any order? More informationAfter asking on the usenet group sci.math.num-analysis I was pointed in the right direction and was told, that the algorithm was known and used in mechanical calculators ("Friede desk calculator") in the early 60s at Boeing already. Later Hewlett Packard sold calculators such as the HP9100, which used a similiar or identical algorithm utilizing BCD (binary coded decimals). I found this reference to the inner workings of the HP Model 9100A and the mathematical background for the algorithm. The background is that: n Σ 2*i - 1 = n2 i=1 so it says that the sum of all 2*i-1 for i starting with 1 up to n is equal to the square of n. I would be interested to learn how to write the corresponding formula for the cubic numbers, any hints? It also seems someone or some people thought it was not possible to transport this algorithm to the binary world. But as I pointed out above, it is not only possible, it is outright simple to do it. So IMHO there is no good reason not to use this algorithm in math libraries, because it should be a lot faster than using Newton's approximation (which involves at least one division for every approximation step), or even more complicated approaches based on logarithms - I guess (IANAM). Have fun Juergen
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||