[ELI5]: How do calculators find out the square root of a number?



[ELI5]: How do calculators find out the square root of a number?

In: Mathematics

Numbers in computers are represented using the “significand” made of significant digits and the base two “exponent” which tells how big the number is. For example, 96 is 3 * 32 or 1.5 * 64 and will be stored as 1.5*2^6 with 1.5 as significand and 6 as exponent.

Square root of 96 is then computed as the square root of 1.5 (as read from a table in memory or with a simple approximation algorithm) multiplied by the square root of 2^6 which is 2^3 or 8 in decimal.

Edit: the approximation algorithm is far out of reach for an ELI5, there are many variations described here : https://en.wikipedia.org/wiki/Methods_of_computing_square_roots

For an ELI45withaPhD on some computation hacks involving simple functions like square root, you can also read : https://en.wikipedia.org/wiki/Fast_inverse_square_root

Depends on the calculator, but here’s one approach, illustrated for square root of 100

1. Make a first estimate (eg half it, your calculator might make a better guess though) = 50.
2. Add that guess to your target divided by that guess, and halve. So (50 + 100/50) / 2 = 26. That’s your next estimate.
3. Continue until the difference between succeeding estimates is small (again depends on your device).

50 -> 26 -> 14.9 -> 10.8 -> 10.03 so it’s getting there very quickly.

Edit: as the other poster said, the actual algorithm used by any given calculator may be a lot more sophisticated.

There’s no ELI5 answer to this as it involves a fair amount of mathematics.

There are many methods of computing a square root, but for the ones that calculators and computers use, [this answer on Quora explains it well](https://www.quora.com/How-do-computers-calculate-square-roots?top_ans=25779191).

If you still want an ELI5 answer to how a square root can be calculated, then here is a simple method. Note that this is *not* the one that a calculator uses as it is too slow.

1. Take an initial guess at the answer, which could be a square root of a nearby square number (for example, 900 = 30^(2), so 30 would be a good first guess for the square root of 1000), or just half the number. It doesn’t have to be that accurate.
2. Square that guess and see whether it is higher or lower than the number you want to find the square root of.
3. Make another guess that gives you a square on the other side of your original number, so that now you have one guess that is too low and one that is too high.
4. Take the average of your two guesses.
5. Square that average. If the square is close enough to the number you want the square root of, then stop.
6. If the square is greater than your original number, replace the high guess with this new guess. If it is lower, replace the low guess with this number.
7. Go back to step 4.

This will give find you the square root as close as you like.

Here’s an example to make it clearer. What’s the square root of 5?

High guess: 3 (because 3^2 is 9, which is greater than 5). Low guess: 2 (2^2 = 4, and 4 < 5).

Average of guesses: (2 + 3)/2 = 2.5

Square of average: 6.25

This is greater than 5, so replace the high guess of 3 with 2.5.

Average of guesses: (2 + 2.5) / 2 = 2.25

Square of average: 5.0625

We are pretty close to 5 now, so 2.25 is a good estimate of the square root, but let’s go one step further. 2.25 is too high, so replace 2.5 with 2.25:

(2 + 2.25) / 2 = 2.125

2.125^2 = 4.515625

This answer is too low, so replace our low guess of 2 with this number:

(2.125 + 2.25) / 2 = 2.1875

2.1875^2 = 4.78515625

and so on until we are happy.

Even though Wikipedia has a gift for explaining it in a way nobody but mathematicians can understand it, many of the methods are really simple:

1. Make a guess
2. Repeatedly adjust the guess until you come closer.

You can easily check if you’re above or below the correct number by squaring (multiplying your guess with itself).

Let’s say I want to calculate the square root of 99. I guess that it’s 1. That’s off by a lot, but that doesn’t matter.

1 * 1 = 1. That’s less than 99, so 1 is too small. Let’s double it.

2 * 2 = 4. Ok, 2 is still too small, let’s double it.

4 * 4 = 16. Still too small.

8 * 8 = 64. Still too small.

16 * 16 = 256. AHA! This is now bigger than 99. That means the square root is between 8 and 16.

Let’s try something in the middle.

12 * 12 = 144

Ok, so the square root is smaller than 12, but bigger than 8. Let’s try something in the middle.

10 * 10 = 100

Ok, so it’s between 8 and 10.

9 * 9 = 81

Ok, between 9 and 10.

9.5 * 9.5 = 90.25

9.75 * 9.75 = 95.0625

9.875 * 9.875 = 97.51

9.9375 * 9.9375 = 98.75

We now know it’s between 9.9375 and 10. The middle between that is 9.96875.

9.96875 * 9.96875 = 99.37

So it’s somewhere between 9.9375 and 9.96875, and if you want to know more exactly, you repeat this a couple dozen more times and you’ll have an answer that’s precise enough for the number of digits your calculator can display. As you can see, we got a pretty good answer with a few steps that I could do manually

This is called a binary search, and works for many other problems too.

The actual algorithm uses a smarter way than “halve the difference”, I’ll write up an example once I’m out of bed and have a real computer and keyboard instead of a phone.