Java牛顿法求方根

/**
 * NewtonsMethod
 * 求n的k方根
 */
public class NewtonsMethod {

    public static void main(String[] args) {
        double n = Double.parseDouble(args[0]);
        double k = Double.parseDouble(args[1]);
        double EPSILON = 1e-15;
        double x = n;

        while(x-n/Math.pow(x, k-1)>EPSILON){
            x = ((k-1)*x+n/Math.pow(x, k-1))/k;
        }
        
        System.out.println(x);

    }
}
发布了1 篇原创文章 · 获赞 0 · 访问量 427

猜你喜欢

转载自blog.csdn.net/weixin_44555861/article/details/104791704