Square root algorithm

Assalamualaikum,

Can anybody help me making a square root algorithm in java?

But I have to use the linear approximation formula...

L(x) = f(a) + f'(a)*(x-a)

where x : the number that we want to take the square root from

a : the nearest point to x, sqrt of a is an integer

f'(a): the derivation of f(a);

f(x) = sqrt(x);

ex. x = 899 --> a = 900

PLEASE HELP ME! i found problems on deciding the nearest point.

[472 byte] By [grimzaa] at [2007-9-28 16:24:36]
# 1

public static double sqrt(double a){

if(a<0) throw new IllegalArgumentException("number<0");

double precision=0.001;

double x_nMinus1 = -1;

double x_n = 1;

while( Math.abs(x_n - x_nMinus1) > precision ) {

x_nMinus1 = x_n;

x_n = (x_nMinus1 * x_nMinus1 + a) / (2*x_nMinus1);

}

return x_n;

}

welkeidkiezena at 2007-7-12 13:33:59 > top of Java-index,Other Topics,Algorithms...