근사 공식

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

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



(*****************************************************) (* Filename: approximatePiOverSqrt12.pas *) (* *) (* Compile: fpc approximatePiOverSqrt12.pas      *) (* Execute: approximatePiOverSqrt12,pas *) (*****************************************************) program ApproximatePiOverSqrt12; var BOUND : integer = 100; value1, value2, err : real; function approximate(BND : integer) : real; (* BND : 급수에서 합할 항의 개수 *) (* Using the series: \frac{\pi}{\sqrt{12}} = \sum^\infty_{k=0} \frac{(-3)^{-k}}{2k+1} *) var i : integer; sum : real = 0; x : real; begin x := 1; sum := 1; for i := 1 to BND do begin x := - (x / 3); sum := sum + x/(2*i + 1); end; approximate := sum; end; begin writeln( 'Approximate the value of PI/sqrt(12)' ); writeln( '-------------------------------------------' ); value1 := PI / SQRT(12.0); writeln( ' Evaluated directly:', value1 ); value2 := approximate(BOUND); writeln( 'Estimated by series:', value2 ); err := value2 - value1; writeln( ' Error:', err ); end.


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



Posted by Scripter

댓글을 달아 주세요