Euler's Number (2018 ACM-ICPC North Central North America Regional Contest)
Euler’s number (you may know it better as just ee) has a special place in mathematics. You may have encountered ee in calculus or economics (for computing compound interest), or perhaps as the base of the natural logarithm, lnxlnx, on your calculator.
While ee can be calculated as a limit, there is a good approximation that can be made using discrete mathematics. The formula for ee is:
e=∑i=0n1i!=10!+11!+12!+13!+14!+⋯e=∑i=0n1i!=10!+11!+12!+13!+14!+⋯
Note that 0!=10!=1. Now as nn approaches ∞∞, the series converges to ee. When nn is any positive constant, the formula serves as an approximation of the actual value of ee. (For example, at n=10n=10 the approximation is already accurate to 77 decimals.)
You will be given a single input, a value of nn, and your job is to compute the approximation of ee for that value of nn.
Input
A single integer nn, ranging from 00 to 1000010000.
Output
A single real number – the approximation of ee computed by the formula with the given nn. All output must be accurate to an absolute or relative error of at most 10−1210−12.
Sample Input 1 | Sample Output 1 |
---|---|
3 |
2.6666666666666665 |
Sample Input 2 | Sample Output 2 |
---|---|
15 |
2.718281828458995 |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int n;
double ans=0,l=1;
scanf("%d",&n);
if(n==0)
{
printf("%.15lf\n",l);
return 0;
}
for(int i=1; i<=n; i++)
{
l/=i;
ans+=l;
}
ans++;
printf("%.15lf\n",ans);
return 0;
}