근사 공식

         \frac{\pi}{\sqrt{12}} = \sum^\infty_{k=0} \frac{(-3)^{-k}}{2k+1}

을 이용하여 근사값 계산하는 프로그램을 Java 언어로 작성해 보았다.



/* * Filename: ApproximatePiOverSqrt12.java * * Compile: javac -d . ApproximatePiOverSqrt12.java * Execute: java ApproximatePiOverSqrt12 */ public class ApproximatePiOverSqrt12 { /* Using the series: \frac{\pi}{\sqrt{12}} = \sum^\infty_{k=0} \frac{(-3)^{-k}}{2k+1} BND : 급수에서 합할 항의 개수 */ public static double approximate(int BND) { int i; double sum = 0; double x; x = 1; sum = 1; for (i = 1; i <= BND; i++) { x = - (x / 3); sum = sum + x/(2*i + 1); } return sum; } public static void main(String[] args) { int BOUND = 100; double value1, value2, err; System.out.println( "Approximate the value of PI/sqrt(12)" ); System.out.println( "-------------------------------------------" ); value1 = Math.PI / Math.sqrt(12.0); System.out.println( " Evaluated directly: " + value1 ); value2 = approximate(BOUND); System.out.println( "Estimated by series: " + value2 ); err = value2 - value1; System.out.println( " Error: " + err ); } }


실행 결과:
Approximate the value of PI/sqrt(12)
-------------------------------------------
 Evaluated directly: 0.9068996821171089
Estimated by series: 0.9068996821171091
              Error: 2.220446049250313E-16



Posted by Scripter
,