#include
int main()
{
int i;
float a=1, sum=1;
for(i=1; a>=1E-5; i++){
a/=i;
sum+=a;
}
printf("e=%f n=%d\n", sum, i+1);
return 0;
}
fabs都用不着,全是正的
#include
#include
long factorial(int x)
{
int i=1;
long res=1;
for(i=1;i<=x;i++)
{
res*=i;
}
return res;
}
int main()
{
int n=1,count=0;
double sum=0;
while(1/factorial(n)>=1e-5)
{
sum+=1/factorial(n);
count++;
}
printf("e的近似值为%lf\n",sum);
printf("一共加了%d项",count);
return 0;
}
∵e^(-6)=1/e^6=0.002478752.
1/5!=0.0083333…
1/6!=0.00138888…
∴可取n=6.
∴e=1+1+1/2!+1/3!+1/4!+1/5!+1/6!
=1+1+0.5+0.166666+0.04166666+0.00833333+0.001388888
=2.718