//varianta cu functie care calculeaza factorialul lui n
int fact(int n)
{
if(n==0) return 1;
else return n*fact(n-1);
}
float e(float eps)
{
float s=0;
int n=0;
while(1.0/fact(n)>=eps)//cat timp ultimul termen >=eps
{
s=s+1.0/fact(n);//il adunam la suma
n++;//marim n
}
return s;
}
//varianta fara functie de factorial
float e2(float eps)
{
float s=1;
int n=1,f=1;
while(1.0/f>=eps)//cat timp ultimul termen >=eps
{
s=s+1.0/f;//adunam ultimul termen
n++;//marim n
f=f*n;//recalculam factorialul
}
return s;
}
|