Square root of a number

Found this method to find the square root of a number interesting – if x is an overestimate of the square root of a number, then number/x should be an underestimate and the mean of this should get you closer to the square root.

Here’s the java code:

double sqrt(double n) {
double previous = 0, current = n/2;

do{
    previous = current;
    current = (previous + n/previous)/2;
} while (previous - current != 0);

return current;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s