% Ramanujan6.m formula for pi by Srinivasa Ramanujan.
% The first term has 5 correct digits.
% Get 6 decimal digits per term after the first.
% We sum only the first 3 terms.
% K. Ming Leung, 02/12/04

clear; format long;

a = 1123;
b = 21460;
c = 882;
s = a/c;
for n=1:2
    n2 = prod(1:2:2*n-1);
    n4 = prod(1:2:4*n-1);
    s = s + (-1)^n*(a+b*n)*n2*n4/(c^(2*n+1)*32^n*(factorial(n))^3);
end
myPi = 4/s,
error = myPi-pi,