Bakhshali Approximation for computing square roots
Bakshali 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++
//This program gives result approximated to 5 decimal places. #include <iostream> float sqroot(float s) { int pSq = 0; //This will be the nearest perfect square to s int N = 0; //This is the sqrt of pSq // Find the nearest perfect square to s 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; //calculate d float P = d/(2.0*N); //calculate P float A = N+P; //calculate A float sqrt_of_s = A-((P*P)/(2.0*A)); //calculate sqrt(S). return sqrt_of_s; } // Driver program to test above function int main() { float num = 9.2345; float sqroot_of_num = sqroot(num); std::cout << "Square root of "<<num<<" = "<<sqroot_of_num; return 0; } |
Java
// Java program gives result approximated // to 5 decimal places. class GFG { static float sqroot(float s) { // This will be the nearest perfect square to s int pSq = 0; //This is the sqrt of pSq int N = 0; // Find the nearest perfect square to s 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; } // calculate d float d = s - pSq; // calculate P float P = d/(2.0f*N); // calculate A float A = N+P; // calculate sqrt(S). float sqrt_of_s = A-((P*P)/(2.0f*A)); return sqrt_of_s; } // Driver program 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); } } // This code is contributed by Anant Agarwal. |
Python3
# This Python3 program gives result # approximated to 5 decimal places. def sqroot(s): # This will be the nearest # perfect square to s pSq = 0; # This is the sqrt of pSq N = 0; # Find the nearest # perfect square to s 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; # calculate d P = d / (2.0 * N); # calculate P A = N + P; # calculate A # calculate sqrt(S). sqrt_of_s = A - ((P * P) / (2.0 * A)); return sqrt_of_s; # Driver Code num = 9.2345; sqroot_of_num = sqroot(num); print("Square root of ", num, "=", round((sqroot_of_num * 100000.0) / 100000.0, 5)); # This code is contributed by mits |
C#
// C# program gives result approximated // to 5 decimal places. using System; class GFG { static float sqroot(float s) { // This will be the nearest // perfect square to s int pSq = 0; //This is the sqrt of pSq int N = 0; // Find the nearest perfect square to s 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; } // calculate d float d = s - pSq; // calculate P float P = d / (2.0f * N); // calculate A float A = N + P; // calculate sqrt(S). float sqrt_of_s = A-((P * P) / (2.0f * A)); return sqrt_of_s; } // Driver Code 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); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // This PHP program gives result // approximated to 5 decimal places. function sqroot($s) { // This will be the nearest // perfect square to s $pSq = 0; //This is the sqrt of pSq $N = 0; // Find the nearest // perfect square to s 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; //calculate d $P = $d / (2.0 * $N); //calculate P $A = $N + $P; //calculate A //calculate sqrt(S). $sqrt_of_s = $A - (($P * $P) / (2.0 * $A)); return $sqrt_of_s; } // Driver Code $num = 9.2345; $sqroot_of_num = sqroot($num); echo "Square root of ". $num ." = " . round(($sqroot_of_num * 100000.0) / 100000.0, 5); // This code is contributed by Sam007 ?> |
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
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Babylonian method for square root
- Find minimum number to be divided to make a number a perfect square
- Magic Square
- Square root of an integer
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Check perfect square using addition/subtraction
- Program to find the Roots of Quadratic equation
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Roots of Unity
- Seeds (Or Seed Roots) of a number
- Nth Square free number
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- How to check if given four points form a square
- Number of times the largest perfect square number can be subtracted from N

