Newton raphson method
public class SqrtDemo {
static double sq_root(double x)
{
double rt = 1, ort = 0;
while(ort!=rt)
{
ort = rt;
rt = ((x/rt) + rt) / 2;
}
return rt;
}
public static void main(String[] args){
int n = args.length;
if(n != 1) {
System.out.println("expected one arguments");
return;
}
double pr = Double.parseDouble(args[0]);
double temp = sq_root(pr);
System.out.println(temp);
}
}
No comments:
Post a Comment