How to calculate a square root

square-root

How to calculate a square root : In this article we will see how to compute the square root of a number

Overview

In mathematics, there are a multitude of operations that have an inverse or opposite operation. For example :

  • Addition is the opposite of subtraction.
  • Multiplication is the inverse of multiplication

For the square root, the inverse is the exponent (i.e. we multiply the number by itself. In this article we will see how to compute :

  • The square root of a perfect number
  • Estimating the square roots of numbers that are not perfect squares without a calculator.
  • How to calculate the square root on the different programming languages.

The square root of a perfect number

The square root of a number is written as follows:

√n : is the number that gives n when multiplied by itself

Example :

√25 = 5 because 5*5=25

Square roots of all the perfect squares from 1 to 1000

Here is the complete list of these numbers:

√0 = 0 because 0x0 = 0
√1 = 1 because 1×1 = 1
√4 = 2 because 2×2 = 4
√9 = 3 because 3×3 = 9
√16 = 4 because 4×4 = 16
√25 = 5 because 5×5 = 25
√36 = 6 because 6×6 = 36
√49 = 7 because 7×7 = 49
√64 = 8 because 8×8 = 64
√81 = 9 because 9×9 = 81
√100 = 10 because 10×10 = 100
√121 = 11 because 11×11 = 121
√144 = 12 because 12×12 = 144
√169 = 13 because 13×13 = 169
√196 = 14 because 14×14 = 196
√225 = 15 because 15×15 = 225
√256 = 16 because 16×16 = 256
√289 = 17 because 17×17 = 289
√324 = 18 because 18×18 = 324
√361 = 19 because 19×19 = 361
√400 = 20 because 20×20 = 400
√441 = 21 because 21×21 = 441
√484 = 22 because 22×22 = 484
√529 = 23 because 23×23 = 529
√576 = 24 because 24×24 = 576
√625 = 25 because 25×25 = 625
√676 = 26 because 26×26 = 676
√729 = 27 because 27×27 = 729
√784 = 28 because 28×28 = 784
√841 = 29 because 29×29 = 841
√900 = 30 because 30×30 = 900
√961 = 31 because 31×31 = 961

To generate these results, I created this python script that extracts the Square roots list of all the perfect squares from 1 to 1000 :

import math

for i in range(1000):
    if math.sqrt(i) % 1 == 0:
        print("√"+str(i)+" = "+str(int(math.sqrt(i)))+" because "+str(int(math.sqrt(i)))+"*"+str(int(math.sqrt(i)))+" = "+str(i))
 

Estimate the square roots of numbers that are not perfect squares

To estimate the square root of a number, we can proceed in 4 steps:

  • The first step : get as close as you can by finding two perfect square roots between which your number is.
  • The second step : divide your number by one of these square roots.
  • The third step : Take the average of the result of step 2 and the root.
  • The fourth step : Use the result of step 3 to repeat steps 2 and 3 until you have a number that is accurate enough for you.

Example :

We want to calculate the square root of 20 :

  1. Thanks to the table defined above, we know that the result of the square root of 20 is between 4 and 5 because 44= 16 and 55 = 25.
  2. Let’s divide 20 by 4. 20/4 = 5
  3. Average of 5 and 4. (5 + 4) / 2 = 4,5
  4. If you want more details in the result, just repeat step 2 and then step 3.

How to calculate the square root on different programming languages

Python sqrt using math module

The math module integrates a function called sqrt which allows to compute the square root of a number :

import math

# Calculate square root of 10
print("square root of 10 : " + str(math.sqrt(10)))

# Calculate square root of 100
print("square root of 100 : " + str(math.sqrt(100)))

# Calculate square root of 100
print("square root of 1000 : " + str(math.sqrt(1000)))
 

Output:

square root of 10 : 3.1622776601683795
square root of 100 : 10.0
square root of 1000 : 31.622776601683793

Java sqrt using java.lang.Math

import java.lang.Math; 
  
class SquareRoot { 
 
    public static void main(String args[]) 
    { 
        double s = 10; 
  
        System.out.println(Math.sqrt(s)); 
  
        s = 100; 
  
        System.out.println(Math.sqrt(s)); 
  
        s = 100; 
  
        System.out.println(Math.sqrt(s)); 
    }
}
  

Square root – PHP sqrt

<?php
echo sqrt(10);
echo'<br>';
echo sqrt(100);
echo'<br>';
echo sqrt(1000);
echo'<br>';
?>

As you can see, most programming languages have predefined functions to compute the square root of a number.

Square Root Calculator



The square root calculator will find the square root of the number you enter :



Square Root Calculator – Javascript Source Code

You will find the javascript source code that allowed me to create this little web tool to calculate the square root :

<script language="JavaScript">

function squareRoot(form) {

var number=form.number.value;

var ans=Math.sqrt(number);

if (number<0) ans="Invalid";

document.getElementById("result").innerHTML = "<br />Square root of "+number+" (√"+number+") : "+ ans;

}
</script>
 
<form name="form">
<input type="text" name="number" size="20">
<input type="button" value="Calculate" onclick="squareRoot(this.form)" name="button">
<p id="result"></p>
</form>
 

Main Menu

Published
Categorized as Learning

By ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Leave a comment

Your email address will not be published.