Table of contents
- What is the square root of a number?
- How do you compute the (fractional) square root of a number by hand?
- What is it good for?
- Let's write some code
- A much much shorter approach (Newton's method)
- Conclusion
What is the square root of a number?
Unlike problems that involve operators like addition or multiplication when you see an expression like the above, many of us quickly reach for our calculators to compute it.
But what exactly is a square root and can we compute it without a calculator?
Take the question What is the square root of 9? Well the square root of 9 is a number when multiplied by itself gives us 9.
So:
- 0 x 0 = 0 nope
- 1 x 1 = 1 nope
- 2 x 2 = 4 nope again
- 3 x 3 = 9 ✅
This means the square root of 9 is 3 because multiplying 3 by itself (3 x 3 = 9) gives us 9.
so then... what about the square root of 2?. Well, there is no whole number when multiplied by itself gives you 2.
No 1 x 1 is still 1 don't think it 😂.
Hint: it's going to be a fraction, we will look at how to compute this in the next section.
Also, why do we need to find the square root of numbers anyway? We will look into that when we are done looking at computing fractional square roots.
How do you compute the (fractional) square root of a number by hand?
Finding fractional square roots is one of the first things that drew me to computers in high school and you will see why as we move along.
Ok, let's say we want to find the square root of 2. In other words, a number when multiplied by itself gives us 2.
At the moment we know the following:
- 0 x 0 = 0
- 1 x 1 = 1
- 2 x 2 = 4
- 3 x 3 = 9
Now looking at this we can tell that 1 x 1 produces 1 which is the closest to our desired result of 2. 2 x 2 produces 4 which is well above 2.
So we can tell the square root of 2 is going to be *1* point something i.e: 1.***
Is it 1.0, 1.1, 1.3, 1.4 1.5? .... well we can find out by multiplying each one by itself.
- 1.0 x 1.0 = 1
- 1.1 x 1.1 = 1.21
- 1.2 x 1.2 = 1.44
- 1.3 x 1.3 = 1.69
- 1.4 x 1.4 = 1.96
- 1.5 x 1.5 = 2.25
From the above, we see that 1.5 x 1.5 produces 2.25 which is a bit over 2, on the other hand, 1.4 x 1.4 produces 1.96 which is much more closer to 2. So again the square root of 2 is roughly 1.4 something.
We can repeat this process with the next decimal place i.e 1.40, 1.41, 1.42, etc to try and get closer or exactly 2
- 1.40 x 1.40 = 1.96
- 1.41 x 1.41 = 1.98
- 1.42 x 1.42 = 2.02
And again you see 1.41 x 1.41 produces 1.98 which is the closest value to 2.
At this point, I am sure you get the approach we are using here. We try out different decimal values to try and end up with 2.
Now do note that the decimal point we are tying out will be any number from 0 to 9, thus the possible tries we can have in our last example will be from 1.40 to 1.49 .
This means we can reduce the number of tries if we start from 1.45 and if it produces a number bigger than 2 we know we should try a lower range 1.44, 1.43, 1.42, 1.41, and vice versa. This halves the number of tries we have to go through.
Given that we have somewhat of an algorithmic approach that has a bit of repetition you can tell this is a very good problem for a computer. So later in this article, we will convert the above approach into code 😊.
For now, let's talk about use cases
What is it good for?
Before we go on I would like to expand the scope of this a bit.
We have been talking about square roots which is WHAT X (same WHAT) gives you 2?. But we can also look at WHAT X WHAT X WHAT gives you 27 (cubic roots) and so on...
Naturally, we do a lot of multiples, additions, and subtractions in our heads, mostly because they have a wide range of practical use cases.
When it comes to square roots I have encountered them when using formulas for solving mostly non everyday problems.
For example, one formula I use from the top of my head is for finding the rough rate of returns of a compounding investment.
So it goes something like this:
- Say I invest $100,000 in a stock
- 3 years later I liquidated the stock at $119,101.60
The question is by what percentage has my investment increased (keeping in mind it's a compounding growth)?
This is what I do in my head:
- 112,360 divided by 100,000 equals 1.1236
- I held it for 2 years so that means I need to find the square root (i.e: 0.5) of 1.1236 thus 1.06
- subtract 1 from 1.059 and I have 1.06
- multiple 1.06 to get the percentage, thus 6%
Hence my investment increased by 6% ish.
FYI sometimes I pull out my calc to do some of the math.
So what are roots good for? well, any formula that contains it and is useful to you 😂.
Let's write some code
It's time to write or rather go through some Python [↗] code.
01: from decimal import Decimal
02:
03: def find_square_root(number, decimal_places, current_result=None, current_decimal_place=1):
04: # Find the integer part of the square root
05: if current_result is None:
06: current_result = int(number / 2)
07:
08: # Check if we have computed the required number of decimal places
09: if current_decimal_place-1 == decimal_places:
10: return current_result
11:
12: # Try placing 0-9 in decimal positions to find the closest square
13: for possible_decimal in range(9, -1, -1):
14: candidate = current_result + Decimal(f'0.{str(possible_decimal).zfill(current_decimal_place)}')
15: squared_result = candidate * candidate
16:
17: if squared_result <= number:
18: return find_square_root(number, decimal_places, candidate, current_decimal_place + 1)
19:
20: # Example usage
21: decimal_places = 3
22: number = 2
23: result = find_square_root(number, decimal_places)
24: print(result) # 1.414
With the steps we went through earlier you might be able to figure out what the code is doing, but let's walk through it real quick, shall we?
So the above code initially takes in a number whose square root we want to find and also to what precision, i.e. how many decimal places.
In the case of the example, we want the square root of 2 to 3 decimal places.
So on
lines 05 to 06
the program checks to see if this is the first time we are running the program so it computes the whole number part of our square root just like we did when we tried to first figure out which whole number when multiplied is closest to 2.The meat of our code is the loop engulfing
lines 14 to 18
this loop is responsible for appending the decimal part of our square root(line 14)
and checking to pick the one decimal closest to 2(line 15 to 17)
Once it's done finding the number for one decimal place it moves on to the next decimal position by calling itself to do the same thing over and over again (recursion [→]).
This recursion is interrupted on
line 05
when it confirms it has found the square root up to the requested decimal place.
And that's pretty much it.
The fun part to write for me was line 14
which shifts the decimal place for the next number. I also used the Decimal module
(line 01)
since using floats to handle decimal numbers in Python has a very limited size.
A much much shorter approach (Newton's method)
Can you believe we can implement the same square root solution but with much less code?
01: from decimal import Decimal
02:
03: number = 2
04: x = (number/2)
05: for _ in range(10000):
06: x = Decimal( x - ( ((x**2)-2) / (2*x) ) )
07:
08: print(x)
This is thanks to a remarkable math formula called Newton's method [↗].
I first discovered this in school, while using my Casio fx-991ES PLUS calculator [↗], it could solve math equations as long as they were solvable.
One thing that caught my attention was how it took more time to solve some equations and went through others rather quickly.
So I decided to look up why and learned it was using Newton's method of equations, which is a formula or rather an approach for finding approximate answers to equations using an iterative approach.
What you do is find the derivative [↗] of the equation you would like to solve and use it as the denominator and use the original equation as the nominator.
You can now pick an arbitrary number in place of X, and then use the resulting number as your X again and again till the value of X comes back as same as befor or till you are tired 😂.
The more you go on the more precise and accurate your answer (X) gets (Not always true though especially when the equation has no solution)
So with the above code I just plugged the formula in there (
line 06
), initialized X to the closest possible square root and ran the loop for a long time (10000 iterations) to get the much more accurate square root.
Conclusion
The decimal position of square root of some numbers go on and on which means you can have your computer keep running to find maore decimal positions by modifying my first example code to handle very very large numbers up until you run out of computer memory.
And thats about it. I will stop typing now. I hope it was worth the read 😊
Here is another article you might like 😊 Creating A Browser-based Interactive Terminal (Using XtermJS And NodeJS)