Bakhshali Approximation for computing square roots in C program


Bakhshali approximation is a method of computing the square root of a number which is not a perfect square. Now, lets brush-related terms to easily understand the concept.

Square root of a number x is a number that satisfies the following condition, y2 = x.

Perfect Square is a number whose square roots are w. For example 16 is perfect square as its roots are 4 and 4.

There are multiple methods defined mathematically to find the square root of a number. In this tutorial, we are going to learn about Bakhshali approximation to find the square root of a number.

It is a method to find approximate roots of of a number. it is equivalent to the first two steps of babylonian method.

working -

The Bakhshali approximation works in the following way,

We have to find the square root of a number s. Below are the steps and calculations that are needed to be done to find this approximation.

  • find the nearest perfect square of the number s,i.e. n2.

  • Find the difference of the number and the nearest perfect square i.e. d = s - n2.

  • Calculate, P = d/(2n).

  • Calculate, A = n + P.

  • The approximate value of square root of s will be (A - P2 / 2A).

Example

#include <iostream>
using namespace std;

int main(){
   float s = 12.3412;
   int perfectSqaure = 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){
            perfectSqaure = i;
            n = j;
            break;
         }
      }
      if (perfectSqaure > 0)
      break;
   }
   float d = s - perfectSqaure;
   float P = d/(2.0*n);
   float A = n+P;
   float rootOfs = A-((P*P)/(2.0*A));
   cout<<"The square root of "<<s<<" = "<<rootOfs;
   return 0;
}

Output

The square root of 12.3412 = 3.51327

Now this approximate square root is very close to the actual root with is 3.51300441. So, this method is quite good for finding approximate square root of the given number. Upto a few places this method is correct so we can use it to find roots of floating point values.

Updated on: 09-Jul-2020

437 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements