Bakhshali Approximation for computing square roots
Last Updated :
18 Apr, 2022
Bakhshali Approximation is a mathematical method of finding an approximation to a square root of a number. It is equivalent to two iterations of Babylonian Method.
Algorithm:
To calculate sqrt(S).
Step 1: Calculate nearest perfect square to S i.e (N2).
Step 2: Calculate d = S - (N2)
Step 3: Calculate P = d/(2*N)
Step 4: Calculate A = N + P
Step 5: Sqrt(S) will be nearly equal to A - (P2/2*A)
Below is the implementation of above steps.
Implementation:
C++
#include <iostream>
float sqroot( float s)
{
int pSq = 0;
int N = 0;
for ( int i = static_cast < int >(s); i > 0; i--)
{
for ( int j = 1; j < i; j++)
{
if (j*j == i)
{
pSq = i;
N = j;
break ;
}
}
if (pSq > 0)
break ;
}
float d = s - pSq;
float P = d/(2.0*N);
float A = N+P;
float sqrt_of_s = A-((P*P)/(2.0*A));
return sqrt_of_s;
}
int main()
{
float num = 9.2345;
float sqroot_of_num = sqroot(num);
std::cout << "Square root of " <<num<< " = " <<sqroot_of_num;
return 0;
}
|
Java
class GFG
{
static float sqroot( float s)
{
int pSq = 0 ;
int N = 0 ;
for ( int i = ( int )(s); i > 0 ; i--)
{
for ( int j = 1 ; j < i; j++)
{
if (j*j == i)
{
pSq = i;
N = j;
break ;
}
}
if (pSq > 0 )
break ;
}
float d = s - pSq;
float P = d/( 2 .0f*N);
float A = N+P;
float sqrt_of_s = A-((P*P)/( 2 .0f*A));
return sqrt_of_s;
}
public static void main (String[] args)
{
float num = 9 .2345f;
float sqroot_of_num = sqroot(num);
System.out.print( "Square root of " +num+ " = "
+ Math.round(sqroot_of_num* 100000.0 )/ 100000.0 );
}
}
|
Python3
def sqroot(s):
pSq = 0 ;
N = 0 ;
for i in range ( int (s), 0 , - 1 ):
for j in range ( 1 , i):
if (j * j = = i):
pSq = i;
N = j;
break ;
if (pSq > 0 ):
break ;
d = s - pSq;
P = d / ( 2.0 * N);
A = N + P;
sqrt_of_s = A - ((P * P) / ( 2.0 * A));
return sqrt_of_s;
num = 9.2345 ;
sqroot_of_num = sqroot(num);
print ( "Square root of " , num, "=" ,
round ((sqroot_of_num * 100000.0 ) / 100000.0 , 5 ));
|
C#
using System;
class GFG {
static float sqroot( float s)
{
int pSq = 0;
int N = 0;
for ( int i = ( int )(s); i > 0; i--)
{
for ( int j = 1; j < i; j++)
{
if (j * j == i)
{
pSq = i;
N = j;
break ;
}
}
if (pSq > 0)
break ;
}
float d = s - pSq;
float P = d / (2.0f * N);
float A = N + P;
float sqrt_of_s = A-((P * P) / (2.0f * A));
return sqrt_of_s;
}
public static void Main ()
{
float num = 9.2345f;
float sqroot_of_num = sqroot(num);
Console.Write( "Square root of " +num+ " = " +
Math.Round(sqroot_of_num * 100000.0) /
100000.0);
}
}
|
PHP
<?php
function sqroot( $s )
{
$pSq = 0;
$N = 0;
for ( $i = intval ( $s ); $i > 0; $i --)
{
for ( $j = 1; $j < $i ; $j ++)
{
if ( $j * $j == $i )
{
$pSq = $i ;
$N = $j ;
break ;
}
}
if ( $pSq > 0)
break ;
}
$d = $s - $pSq ;
$P = $d / (2.0 * $N );
$A = $N + $P ;
$sqrt_of_s = $A - (( $P * $P ) /
(2.0 * $A ));
return $sqrt_of_s ;
}
$num = 9.2345;
$sqroot_of_num = sqroot( $num );
echo "Square root of " . $num . " = " .
round (( $sqroot_of_num *
100000.0) /
100000.0, 5);
?>
|
Javascript
<script>
function sqroot(s)
{
var pSq = 0;
var N = 0;
for (i = parseInt(s); i > 0; i--)
{
for (j = 1; j < i; j++)
{
if (j*j == i)
{
pSq = i;
N = j;
break ;
}
}
if (pSq > 0)
break ;
}
var d = s - pSq;
var P = (d/(2.0*N));
var A = N+P;
var sqrt_of_s = A-((P*P)/(2.0*A));
return sqrt_of_s;
}
var num = 9.2345;
var sqroot_of_num = sqroot(num);
document.write( "Square root of " +num+ " = "
+ (Math.round(sqroot_of_num*100000.0)/100000.0).toFixed(6));
</script>
|
Output :
Square root of 9.2345 = 3.03883
Illustration:
find sqrt(9.2345)
S = 9.2345
N = 3
d = 9.2345 – (3^2) = 0.2345
P = 0.2345/(2*3) = 0.0391
A = 3 + 0.0391 = 3.0391
therefore, sqrt(9.2345) = 3.0391 – (0.0391^2/(2*0.0391)) = 3.0388
Important Points:
- Used to find approximation to a square root.
- Requires the value of nearest perfect square of the number whose square root is needed to be calculated.
- More efficient for floating point numbers than integers as it finds approximation.
Reference:
https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Bakhshali_approximation
Share your thoughts in the comments
Please Login to comment...