근사 공식

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

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



/* * Filename: ApproximatePiOverSqrt12.cs * * Compile: csc ApproximatePiOverSqrt12.cs * Execute: ApproximatePiOverSqrt12 */ using System; 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; Console.WriteLine( "Approximate the value of PI/sqrt(12)" ); Console.WriteLine( "-------------------------------------------" ); value1 = Math.PI / Math.Sqrt(12.0); Console.WriteLine( " Evaluated directly: " + value1 ); value2 = Approximate(BOUND); Console.WriteLine( "Estimated by series: " + value2 ); err = value2 - value1; Console.WriteLine( " Error: " + err ); } }


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


Posted by Scripter
,