利用牛顿开方法开任意次方
2015-06-05 21:32
274 查看
牛顿开方法原理在http://www.guokr.com/question/461510/有详细介绍,只需要记住下面的公式即可;
以下是根据上述公式写的代码,其中n表示迭代次数,a表示被开方数,k表示开方次数
以下是根据上述公式写的代码,其中n表示迭代次数,a表示被开方数,k表示开方次数
package ibbe.scheme.keydistrubution; import java.lang.Math; public class Nuton { public static double root(int n) { double k = 2; double a = 3; double flg = 0; if (n == 0) { return 1; } else { // flg=Math.pow(root(n-1), k-1); return ((k - 1) / k) * root(n - 1) + (a / (k * (Math.pow(root(n - 1), k - 1)))); } } // 用来求2次方根,这个是百度到的求2次方根的代码 static double NewtonMethod(double fToBeSqrted) { double x = 1.0; while (Math.abs(x * x - fToBeSqrted) > 1e-5) { x = (x + fToBeSqrted / x) / 2; } return x; } public static void main(String[] args) { System.out.println(root(7)); System.out.println(NewtonMethod(2)); } }运行结果:
相关文章推荐
- 【分享】失忆症AMNESIA和LATER汉化游戏下载+图文攻略+全开存档
- [置顶]1244-作为一个java开发者的知识储备
- mysql 架构与概念
- leetcode_PermutationSequence
- Structs 整合Spring
- 如何把批量数据导入到android 的 sqlite 数据库(转)
- 如何修改excel转换csv的分隔符
- Android完全退出应用程序(一)
- 反射机制
- How to Reference and Use JSTL in your Web Application
- a标签中的href,onclick,#,javascript:void简要分析 (转载)
- "XX cannot be resolved to a type " eclipse报错及解决说明
- eclipse里不支持泛型的解决方法
- iframe自适应高度的多种方法方法小结
- office 2003——office 2007 mime 类型
- Tomcat服务器自动加载监听程序(转载)
- 【转载】十年学会编程
- 【转载】补充myeclipse10 优化
- Win7+CentOS 7双系统启动顺序修改方法.
- Win7+CentOS 7双系统启动顺序修改方法.