Saturday 20 March 2021

An Old Fashioned Way To Calculate Square Roots

In 1974, long before I had ever seen a computer, I learnt how to work out square roots using an electronic calculator. I have implemented this method in the simple Java program below to calculate the square root of 10.
 
It starts by choosing an arbitrary value, which is 2 in this case, then uses a do...while loop to perform the following steps in each iteration:
  1. Store the old square root in root1.
  2. Divide 10 by root1 and store the result in root2.
  3. Calculate the average of root1 and root2 and assign this to root3, which becomes the new square root.
  4. Use Math.abs to calculate the absolute difference between the old and new square roots then express this as a percentage.
It continues doing this until the percentage difference is small enough:
 
 
Then I used the calculator on a Windows 7 PC and got the same result:
 

I moved to the south east of England in 1983 to start my career in IT and stayed in a bedsit for a few months. One of the other rooms in the house was also occupied by a programmer and we would occasionally discuss algorithms. I asked him if he knew how numbers like pi or the square root of 2 (for example) were calculated to many decimal places. He explained that the individual digits in such numbers would be stored in arrays and arithmetical calculations would be carried out long hand. This made sense but I was unable to take it any further at the time. It occurred to me that addition and subtraction might be easy enough but long multiplication would take longer to program and long division would be extremely difficult.

I recently found out that Python is able do calculations to great precision and decided to teach myself this language. After a few days I used the method described above to write the program below to work out the square root of two to a little over one million decimal places:

andrew@ASUS-Laptop:~/Python/ROOT2$ cat root2k.py
import mpmath
mpmath.mp.dps = 1000200

root3 = mpmath.mpf (1)
difference = mpmath.mpf (1)

while difference > mpmath.power(10,-1000199):
    root1 = mpmath.mpf (root3)
    root2 = mpmath.mpf (2 / root1)
    root3 = mpmath.mpf ((root1 + root2)/2)
    difference = mpmath.fabs (root3 - root1)
    if difference <= mpmath.power(10,-1000199):
        print ("The square root of two = ")
        print " ", root3
andrew@ASUS-Laptop:~/Python/ROOT2$

I ran it like this:

andrew@ASUS-Laptop:~/Python/ROOT2$ date;python root2k.py|fold -w80 > my_output;date
Mon Oct 25 16:26:10 EDT 2021
Mon Oct 25 17:00:31 EDT 2021
andrew@ASUS-Laptop:~/Python/ROOT2$

It took just over 30 minutes to run on an UBUNTU laptop with a Celeron processor. Here is the start of my output (as usual, click on the image to enlarge it / bring it into focus if necessary):

Here is the end of my output:

I looked online and found that NASA calculated the square root of two to just over one million decimal places around twenty years ago. You can see their results here. I contacted one of the authors and he confirmed that these results have been checked many times already. Fortunately my results are much the same. The only difference being that, at the end, I have 43 extra decimal places.

I later noticed that NASA also calculated the square root of two to ten million decimal places here. I used this to confirm that my extra 43 decimal places were correct.

No comments:

Post a Comment