how to test if a num is a square root?
ex: 9 is square root
3 is not square root
/////////////////////////////////////////////
this algorithm works fine when y is small int. ex 4, 10, 20.
but when i put y to be large ex 10000. than i get it to problem.
this is bc x start out with 0 and slowing get inc by 1. this takes alot of time.
is there better way to do this by using recursion? may be there is some formula that i dont know about.
ex: 9 is square root
3 is not square root
/////////////////////////////////////////////
Code:
x = 0; //always start at 0
y = 101010; //is this a square root?
square(x, y){
if((x*x) >= y){ //not a square root
return false;
}
else if((x*x) == y) //it is a square root
return true;
}
else{ //inc x by 1
square(x+1, y);
}
}
but when i put y to be large ex 10000. than i get it to problem.
this is bc x start out with 0 and slowing get inc by 1. this takes alot of time.
is there better way to do this by using recursion? may be there is some formula that i dont know about.
Comment