2013
11-09

# u Calculate e

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.

No input

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.

no input

n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
...

//* @author: 82638882@163.com
public class Main
{
public static void main(String[] args)	{

int i, n, j;
double e = 0;

System.out.printf("n e\n- -----------\n");
for (n = 0; n <= 9; ++n)
{
i = 1;
for (j = 1; j <= n; ++j)
i *= j;
e += 1.0 / i;
System.out.printf("%d %10.9f\n", n, e);
}

}
}

1. 有两个重复的话结果是正确的，但解法不够严谨，后面重复的覆盖掉前面的，由于题目数据限制也比较严，所以能提交通过。已更新算法

2. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count