And the lack of decimals.
Anyway, here is a way to do it without using iterations (so can be slow for LARGE numbers, but should work for most cases), it can return the square root rounded to the nearest integer (tested):
function sqrt(int num){
num=abs(num); //well you may use the same abs()
int i=0;
while (i*i<num) i++;
int temp = i*i-num;
if (temp){
i--;
temp = (num-(i*i))*4;
if (temp>(4*i)+1)i++;
}
return i;
}
Anyway, here is a way to do it without using iterations (so can be slow for LARGE numbers, but should work for most cases), it can return the square root rounded to the nearest integer (tested):
function sqrt(int num){
num=abs(num); //well you may use the same abs()
int i=0;
while (i*i<num) i++;
int temp = i*i-num;
if (temp){
i--;
temp = (num-(i*i))*4;
if (temp>(4*i)+1)i++;
}
return i;
}