Number Pi

PROBLEM
Compute the number Pi to thousand decimal places.

ALGORITHM
Algorithm of J. M. Borwein and P. B. Borwein for computation of Pi number starts with the initializations

X=SQRT(2); Pi=2+X; Y=SQRT(X)

and then repeats the iteration

X = 0.5 * (SQRT(X) + 1 / SQRT(X)
Pi = Pi * (X + 1) / (Y + 1)
Y = Y * (SQRT(X) + 1 / SQRT(X)) / (Y + 1)

IMPLEMENTATION
Unit: internal function, external function without procedure statement
 
Interface: SQRT function
 
Parameter: a positive integer P - number of digits of Pi, implicit P=9
 
Returns: the first P significant decimal digits of Pi, default is 9

 


PI: procedure
parse arg P
if P = "" then P = 9; numeric digits P
X = SQRT(2, P); Pi = 2 + X
Y = SQRT(X, P); X = Y
do forever
  X = 0.5 * (X + 1 / X)
  NewPi = Pi * (X + 1) / (Y + 1)
  if Pi = NewPi then return Pi
  Pi = NewPi
  X = SQRT(X, P)
  Y = (Y * X + 1 / X) / (Y + 1)
end

 

There is an another way to obtain the value of the number Pi: we can compute the number Pi only once and save its value into the text of the function. The following PICONST function can be used to computing of the first P decimal digits of Pi, for P=1,...,200. It was created automatically by the program, see Technique: Beforehand computed constants.
 


PICONST: procedure; V = ''
V = V || 3.14159265358979323846264338327950288419
V = V || 7169399375105820974944592307816406286208
V = V || 9986280348253421170679821480865132823066
V = V || 4709384460955058223172535940812848111745
V = V || 028410270193852110555964462294895493038196
return V

 

The statement Number_Pi=PI(1000) computes 10 times loop in PI function and requires 79 seconds.

CONNECTIONS
Square root of positive number
Technique: Beforehand computed constants
P+n trick by Walter Pachl in Reflexio

Literature
Press W.H., Teukolsky S.A., Vetterling W.T., Flannery B.P. Numerical Recipes in C : the art of scientific computing
- 2nd ed. University Press, Cambridge, 1992


Cover Contents Index Main page Rexx page   Mail

last modified 30th July 2001
Copyright © 2000-2001 Vladimir Zabrodsky
Czech Republic
 

1