Simple way to find square root of perfect squares

76 Views Asked by At

Let me first explain my problem:

I am trying to write a program that can generate operations that compare a set of data rather than pulling from a list of possible relations. I have it to the point where it can generate exponentiation but I think it might be easier to generate the square root algorithm by reversing the generated exponentiation. To do this I need to understand a simple way to find square roots from perfect squares (they will be perfect, I am using integer math right now).

The simpler the better but I am not looking for something that searches every possibility. I need an algorithm.

Thanks

1

There are 1 best solutions below

0
On

Newton's method for computing $\sqrt n$ is fast and simple to implement. For example, here is C code to do it:

int integer_sqrt(int n){
  int x,oldx;
  if(n==0) return 0;
  x=n;
  do{
    oldx=x;
    x=(x+n/x)/2;
  }while(x!=oldx);
  return x;
}