Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding square root without using sqrt function?

Tags:

I was finding out the algorithm for finding out the square root without using sqrt function and then tried to put into programming. I end up with this working code in C++

    #include <iostream>     using namespace std;      double SqrtNumber(double num)     {              double lower_bound=0;               double upper_bound=num;              double temp=0;                    /* ek edited this line */               int nCount = 50;          while(nCount != 0)         {                temp=(lower_bound+upper_bound)/2;                if(temp*temp==num)                 {                        return temp;                }                else if(temp*temp > num)                 {                        upper_bound = temp;                }                else                {                        lower_bound = temp;                }         nCount--;      }         return temp;      }       int main()      {      double num;      cout<<"Enter the number\n";      cin>>num;       if(num < 0)      {      cout<<"Error: Negative number!";      return 0;      }       cout<<"Square roots are: +"<<sqrtnum(num) and <<" and -"<<sqrtnum(num);      return 0;      }  

Now the problem is initializing the number of iterations nCount in the declaratione ( here it is 50). For example to find out square root of 36 it takes 22 iterations, so no problem whereas finding the square root of 15625 takes more than 50 iterations, So it would return the value of temp after 50 iterations. Please give a solution for this.

like image 843
Arun Pandey Avatar asked Oct 26 '13 19:10

Arun Pandey


People also ask

How do you find the square root without guessing?

Try it: +2 × +2 = 4 and -2 × -2 = 4. Since a square root of a number must equal that number when multiplied by itself. When you multiply this number by itself, and set it up as a full equation ( n * n = x ), the two factors (n and n) are either both positive or both negative since they are the same number.


2 Answers

There is a better algorithm, which needs at most 6 iterations to converge to maximum precision for double numbers:

#include <math.h>  double sqrt(double x) {     if (x <= 0)         return 0;       // if negative number throw an exception?     int exp = 0;     x = frexp(x, &exp); // extract binary exponent from x     if (exp & 1) {      // we want exponent to be even         exp--;         x *= 2;     }     double y = (1+x)/2; // first approximation     double z = 0;     while (y != z) {    // yes, we CAN compare doubles here!         z = y;         y = (y + x/y) / 2;     }     return ldexp(y, exp/2); // multiply answer by 2^(exp/2) } 

Algorithm starts with 1 as first approximation for square root value. Then, on each step, it improves next approximation by taking average between current value y and x/y. If y = sqrt(x), it will be the same. If y > sqrt(x), then x/y < sqrt(x) by about the same amount. In other words, it will converge very fast.

UPDATE: To speed up convergence on very large or very small numbers, changed sqrt() function to extract binary exponent and compute square root from number in [1, 4) range. It now needs frexp() from <math.h> to get binary exponent, but it is possible to get this exponent by extracting bits from IEEE-754 number format without using frexp().

like image 122
mvp Avatar answered Oct 03 '22 15:10

mvp


Why not try to use the Babylonian method for finding a square root.

Here is my code for it:

double sqrt(double number) {     double error = 0.00001; //define the precision of your result     double s = number;      while ((s - number / s) > error) //loop until precision satisfied      {         s = (s + number / s) / 2;     }     return s; } 

Good luck!

like image 38
Newskooler Avatar answered Oct 03 '22 15:10

Newskooler