Calculating Square Root of Integer, PHP Code

### Author Topic: Calculating Square Root of Integer, PHP Code  (Read 3690 times)

#### msu_math

• Jr. Member
• Posts: 81
##### Calculating Square Root of Integer, PHP Code
« on: November 21, 2012, 06:37:27 PM »
The function SquareRoot( \$number ) returns square root of integer up to 4 decimal places.

function SquareRoot( \$number )
{
\$number = \$number."00000000";
\$number_length = strlen(\$number);
if(\$number_length % 2 == 0)
{
\$dividant = \$number[0].\$number[1];
for(\$i=2;\$i<\$number_length;\$i++)
{
\$number[\$i-2] = \$number[\$i];
}
\$number[\$number_length-1] = NULL;
\$number[\$number_length-2] = NULL;
\$number_length = \$number_length-2;
}
else
{
\$dividant = \$number[0];
for(\$i=1;\$i<\$number_length;\$i++)
{
\$number[\$i-1] = \$number[\$i];
}
\$number[\$number_length-1] = NULL;
\$number_length = \$number_length-1;
}
\$root = 0;
while(\$dividant != 0)
{
\$multiplier = 0;
\$product = 0;
while(\$product <= \$dividant)
{
\$divisor = \$root*2;
\$multiplier = \$multiplier+1;
\$divisor = \$divisor*10+\$multiplier;
\$product = \$divisor*\$multiplier;
}
\$multiplier = \$multiplier-1;
\$divisor = \$root*20+\$multiplier;
\$root = \$root*10+\$multiplier;
\$square_root = \$square_root.\$multiplier;
if(\$number_length==8)
{
\$square_root = \$square_root.".";
}
\$product = \$divisor*\$multiplier;
\$dividant = \$dividant-\$product;
if(\$number_length <= 0)
{
break;
}
\$dividant = \$dividant.\$number[0].\$number[1];
for(\$i=2;\$i<\$number_length;\$i++)
{
\$number[\$i-2] = \$number[\$i];
}
\$number[\$number_length-1] = NULL;
\$number[\$number_length-2] = NULL;
\$number_length = \$number_length-2;
}
return \$square_root;
}
« Last Edit: November 21, 2012, 07:18:49 PM by msu_math »

Lecturer in Mathematics
Department of Natural Sciences
FSIT, DIU

#### tamim_saif

• Sr. Member
• Posts: 357
• Test
##### Re: Calculating Square Root of Integer, PHP Code
« Reply #1 on: November 21, 2012, 07:46:41 PM »
Thanks. You may include sample input and sample output images for more clear Illustration for the students.

#### msu_math

• Jr. Member
• Posts: 81
##### Re: Calculating Square Root of Integer, PHP Code
« Reply #2 on: November 22, 2012, 12:08:30 PM »
Sample Return value of SquareRoot():

Call: SquareRoot( 562 );     Return value:  23.7065

Steps in Division Method:
---------------------------------------------------------------------------------------
1. Dividant = 5,               Divisor = 2,              Square Root = 2
---------------------------------------------------------------------------------------
2. Dividant = 162,           Divisor = 43,            Square Root = 23
---------------------------------------------------------------------------------------
3. Dividant = 3300,         Divisor = 467,          Square Root = 23.7
---------------------------------------------------------------------------------------
4. Dividant = 3100,         Divisor = 4740,        Square Root = 23.70
---------------------------------------------------------------------------------------
5. Dividant = 310000,     Divisor = 47406,      Square Root = 23.706
---------------------------------------------------------------------------------------
6. Dividant = 2556400,   Divisor = 474125,    Square Root = 23.7065
---------------------------------------------------------------------------------------
« Last Edit: November 22, 2012, 12:45:03 PM by msu_math »