Ramanujan and Salamin-Brent Methods for π

MATH 242 Modern Computational Mathematics

Ramanujan's Method

Here is Ramanujan's sum (equation (1.12) on page 33 in the text): 
(1/π)=((2(Sqrt[2]))/9801)  (∑_(k=0)^∞)(((4k)!(1103+26390k))/((((k!))^4)(396^(4k))))

Complete the following module to add up the first m terms of Ramanujan’s sum and return an approximation of π:

ramanujan[m_]:=Module[

Do[
sum+=(((4k)!(1103+26390k))/((k!)^4*396^(4k))),
{k,0,m}];


]

Demonstrate that your module returns exact values that are close to π.

How many correct digits of π are produced by each term of Ramanujan’s sum?

nMax=20;
piValuesRamanujan=

accuracyRamanujan=

ListPlot[accuracyRamanujan,AxesLabel->{"n","correct digits"}]

TableForm[Transpose[{Range[nMax],accuracyRamanujan}],TableHeadings->{None,{"terms","correct digits"}}]

Indexed Variables in Mathematica

Mathematica lets you type variables with subscripts, but these are used for display purposes and not for creating a list of variables in a computation. For example, consider the following:

Clear[a];
(Subscript[a, 1])=3
(Subscript[a, 2])=7

We might that the previous code has defined two variables associated with the symbol a, but this is not the case. We can see this if we query the symbol a, like this:

?a

If there were values assigned to the symbol a, we would see them in the previous output, but we don’t see any values there.

Instead, our subscripted variables are actually stored as patterns associated with the Subscript object, as we can see by running the following code.

DownValues[Subscript]

If you want to create indexed variables for use in a computation, you should put the index in square brackets following the variable name. (This is the same way that you would define individual values of a function.) Here is an example:

Clear[b]
b[1]=3
b[2]=7

Now these values are actually associated with the symbol b, which we can see as follows:

?b

We can also access these values by using an index variable within the square brackets, provided that this index variable evaluates to one of the indexes that we defined:

Do[
Print[  b[i]  ],
{i,1,2}]

If you want to define indexed variables in a module, only the “head” of the variables (that is, the name that occurs before the square brackets) needs to go in the local variable list. Here is an example:

demoModule[]:=Module[
{a},

(* define some indexed variables *)
a[1]=2;
a[2]=5;
a[4]=8;

(* return the values as a list *)
{a[1],a[2],a[4]}
]

demoModule[]

Let's clear the symbols we’ve defined in this demonstration:

Clear[a, b,demoModule, Subscript]

Salamin-Brent Algorithm

Implement a module that computes approximations of π using the Salamin-Brent method (Algorithm 1.23 on page 36 in the text). Your module should accept one input parameter: an integer that specifies how many iterations of the algorithm to compute. Make a table showing how the number of correct digits of π increases with the number of terms that you add up. Answer the question: What is the relationship between the number of iterations and the number of correct digits of π?