; ; Calculates the Wallis product to find an approximation of Pi: ; ; Pi/2 = 2 * 2 * 3 * 3 * 4 * 4 ... ; /1 /3 /3 /4 /4 /5 ... ; ; As the terms get very large, rounding errors in dividing ; large integers causes the approximation to drift away from the true ; limiting value of the product. The rounding errors are due to limited ; precision of the standard computer representation of real numbers. ; ; See http://en.wikipedia.org/wiki/Wallis_product for more info about ; the Wallis product. ; ; Jacob, May 3, 2008 ; Modified by Mark Dow May 4, 2008 (comments, output at a range of terms) ; AppTitle( "Wallis Product" ) Graphics( 600, 600, 16, 2 ) Global nEven = 2 ; numerator Global nOdd = 3 ; denominator component Global flPi1# = 0 ; odd terms Global flPi2# = 0 ; even terms Global flPi3# = 2 ; two times the current product, so the result ; is an approximation of Pi Global nRun = 0 ; Main loop: While ( nRun <= 100000000 ) nRun = nRun + 1 ; Find the next two terms each time through the main loop: flPi1# = 1.0 * nEven/( nOdd - 2 ) ; multiply these by 1.0 so the result is a real number flPi2# = 1.0 * nEven/ nOdd ; Calculate product with the next two terms: flPi3# = flPi3# * flPi1# * flPi2# ; Increment the numerator an denominator factors nOdd = nOdd + 2 nEven = nEven + 2 ; Print the current approximation at every nth term, ; where n is determined by the logarithm of n. If ( nRun <= 10 And nRun Mod 2 = 0 ) Print ( "The calculated value of Pi is " + flPi3# + ", at the " + 2*nRun + "th term" ) EndIf If ( nRun <= 100 And nRun Mod 20 = 0 ) Print ( "The calculated value of Pi is " + flPi3# + ", at the " + 2*nRun + "th term" ) EndIf If ( nRun <= 1000 And nRun Mod 200 = 0 ) Print ( "The calculated value of Pi is " + flPi3# + ", at the " + 2*nRun + "th term" ) EndIf If ( nRun <= 10000 And nRun Mod 2000 = 0 ) Print ( "The calculated value of Pi is " + flPi3# + ", at the " + 2*nRun + "th term" ) EndIf If ( nRun <= 100000 And nRun Mod 20000 = 0 ) Print ( "The calculated value of Pi is " + flPi3# + ", at the " + 2*nRun + "th term" ) EndIf If ( nRun <= 1000000 And nRun Mod 200000 = 0 ) Print ( "The calculated value of Pi is " + flPi3# + ", at the " + 2*nRun + "th term" ) EndIf If ( nRun <= 10000000 And nRun Mod 2000000 = 0 ) Print ( "The calculated value of Pi is " + flPi3# + ", at the " + 2*nRun + "th term" ) EndIf If ( nRun <= 100000000 And nRun Mod 20000000 = 0 ) Print ( "The calculated value of Pi is " + flPi3# + ", at the " + 2*nRun + "th term" ) EndIf ;Stop Wend ; end of main loop Stop End