2012年3月14日20:37:40 今天是世界圆周率日 上Java算π的算法 1000位的
2012-03-14 20:35
369 查看
import java.math.BigDecimal; public class PI { public static void main(String args[]) throws NumberFormatException { int digits = 1000; // 计算1000位 long begin = System.currentTimeMillis(); String pi = computePi(digits).toString(); long end = System.currentTimeMillis(); System.out.println(pi); System.out.println((end - begin) + "ms"); } /** constants used in pi computation */ private static final BigDecimal FOUR = BigDecimal.valueOf(4); /** rounding mode to use during pi computation */ private static final int roundingMode = BigDecimal.ROUND_HALF_EVEN; /** * Compute the value of pi to the specified number of digits after the * decimal point. The value is computed using Machin's formula: * * pi/4 = 4*arctan(1/5) - arctan(1/239) * * and a power series expansion of arctan(x) to sufficient precision. */ public static BigDecimal computePi(int digits) { int scale = digits + 5; BigDecimal arctan1_5 = arctan(5, scale); BigDecimal arctan1_239 = arctan(239, scale); BigDecimal pi = arctan1_5.multiply(FOUR).subtract(arctan1_239) .multiply(FOUR); return pi.setScale(digits, BigDecimal.ROUND_HALF_UP); } /** * Compute the value, in radians, of the arctangent of the inverse of the * supplied integer to the specified number of digits after the decimal * point. The value is computed using the power series expansion for the arc * tangent: * * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + (x^9)/9 ... */ public static BigDecimal arctan(int inverseX, int scale) { BigDecimal result, numer, term; BigDecimal invX = BigDecimal.valueOf(inverseX); BigDecimal invX2 = BigDecimal.valueOf(inverseX * inverseX); numer = BigDecimal.ONE.divide(invX, scale, roundingMode); result = numer; int i = 1; do { numer = numer.divide(invX2, scale, roundingMode); int denom = 2 * i + 1; term = numer.divide(BigDecimal.valueOf(denom), scale, roundingMode); if ((i % 2) != 0) { result = result.subtract(term); } else { result = result.add(term); } i++; } while (term.compareTo(BigDecimal.ZERO) != 0); return result; } }
console:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
39ms
相关文章推荐
- 隐马尔可夫模型的前向算法(java实现),今天奉上
- 算法的威力:法国人用单台台式机打破由超级计算机创造的圆周率运算世界记录
- 各种求圆周率π的算法(蒙特卡洛法的Java实现)
- 算法的威力:法国人用台式机打破由超级计算机创造的圆周率运算世界记录
- Java实例12 - 圆周率π的随机数算法
- 算法的威力:法国人用台式机打破由超级计算机创造的圆周率运算世界记录
- 各种求圆周率π的算法(蒙特卡洛法的Java实现)
- zz 算法的威力:法国人用台式机打破由超级计算机创造的圆周率运算世界记录
- Java Collection框架—List\ set \map 的异同世界
- 【老鸟学算法】大整数乘法——算法思想及java实现
- 《剑指Offer》java 2.4 算法和数据操作
- 《深入理解JAVA虚拟机》学习笔记(三)JAVA垃圾收集算法和常见垃圾收集器
- java常用算法之快速排序详解
- JAVA实现简单抢红包算法(模拟真实抢红包)
- 基础算法(三)---快速排序(Java)
- Jodd - Java世界的瑞士军刀
- 基于仿射传播聚类和最近邻算法的WiFi室内定位源码,采集java,c#,定位matlab
- 数据挖掘十大经典算法之Apriori算法以及Java实现
- Java 实现微信红包分配算法
- Java经典算法40例(二十六)