[math]e = \frac{1 + \sqrt{1 + \alpha\beta}}{\alpha}[/math]

where

[math]\alpha = \sum_{n=0}^{\infty}\sum_{i=n}^{2n}\sum_{k=0}^{2(i-n)} (-1)^{n+i+k}\frac{ (2n)!(2i)!(4n+1)}{16^n (2n-i)!(n!)^2 i! k!}[/math]

and

[math]\beta = \sum_{n=0}^{\infty}\sum_{i=n}^{2n}\sum_{k=0}^{2(i-n)} (-1)^{n+i}\frac{ (2n)!(2i)!(4n+1)}{16^n (2n-i)!(n!)^2 i! k!}[/math]

I try to implement this using some code. For n = 0..2, I get 2.718281 57... (compared to the correct value of 2.718281 83...), which is about as good as I could have hoped for. However, for n = 0..3, I get 2.835..., and for higher n the number I get quickly converges to 2.83424495... While it's certainly possible that my formula is wrong, or that I implemented it wrong, the fact that I get such a good result for n = 0..2 makes me wonder if this isn't a problem with c handling numbers. Can someone please look at my code and see if there might be such problems somewhere, and if there's anything could do to fix them? Thanks.

Code: Select all

`#include <stdio.h>`

#include <stdlib.h>

#include <string.h>

#include <time.h>

#include <math.h>

int fact (int n)

{

int prod;

if(n!=0)

{

prod = n*fact(n-1);

}

else

{

prod = 1;

}

return prod;

}

int main (void)

{

int nmax = 15;

int imin, imax, kmin, kmax;

int n = 0, i = 0, k = 0;

double term, terma, termb;

double alpha = 0, beta = 0;

double e;

double moredigits;

for(n = 0; n < nmax + 1; n++)

{

imin = n;

imax = 2*n;

for(i = imin; i < imax + 1; i++)

{

kmin = 0;

kmax = 2*(i-n);

for(k = kmin; k < kmax + 1; k++)

{

term = (4*n + 1)*fact(2*n)*fact(2*i) / ( pow(16,n)*pow(fact(n),2)*fact(2*n-i)*fact(i)*fact(k) );

terma = term*pow((-1), (n+i+k)%2);

termb = term*pow((-1), (n+i)%2);

alpha += terma;

beta += termb;

}

}

}

e = (1 + pow((1+alpha*beta), 0.5))/alpha;

/*printf("%f ", e);*/

moredigits = (e*1000000);

printf("%f", moredigits);

}

(My apologies for the lack of documentation. Let me know if there's anything you want clarified.)