poj 1517 u Calculate e
u Calculate e
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 14442
Accepted: 8745
Special Judge
Description
A simple mathematical formula for e is
e=Σ0<=i<=n1/i!
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
Input
No input
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 Input
no input
Sample Output
n e - ----------- 0 1 1 2 2 2.5 3 2.666666667 4 2.708333333 ...
1: #include<iostream>
2: using namespace std;
3: int main()
4: {
5: int i;
6: double res;
7: int divi;
8: printf("n e\n");
9: printf("- -----------\n");
10: printf("0 1\n");
11: printf("1 2\n");
12: printf("2 2.5\n");
13: divi=2;
14: res=2.5;
15: for(i=3;i<=9;i++)
16: {
17: divi*=i;
18: res+=1.0/divi;
19: printf("%d %11.9f\n",i,res);
20: }
21: return 0;
22: }
23: