1012.u Calculate e
LanyuanXiaoyao's Blog ヽ(✿゚▽゚)ノ

1012.u Calculate e

2014-10-03

Problem

Problem Description
A simple mathematical formula for e is where n is

allowed to go to infinity.
This can actually yield very accurate approximations of e using relatively small values of n.
Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
Sample Output
n e
- ———–
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333

Solution

#include<stdio.h>
int main(void)
{
    double ji,sum;
    int i,j,k;
    
    printf("n e\n");
    printf("- -----------\n");
    
    for(i=0;i<=9;i++){
        sum=0;
        for(j=0;j<=i;j++){
            ji=1;
            for(k=1;k<=j;k++){
                ji=ji*1/k;
            }
            sum=sum+ji;
        }
        if(i<=1){
            printf("%d %.0f\n",i,sum);
        }
        else if(i==2){
            printf("%d %.1f\n",i,sum);
        }
        else{
            printf("%d %.9f\n",i,sum);
        }
    }
    return 0;
} 

评论