题目
Java实现
实现1
public static double findE(){
double e = 1;
int n = 1; //1,2,3,4,...
double k = 1; //第n项值
while (k > 1e-6){
//计算阶乘
int j = 1;
for (int i = n; i > 1 ; i--) {
j *= i;
}
k = 1/(double)j;
e += k;
n++;
}
return e;
}
实现2
public static double findE2(){
double e = 0;
int n = 1; //1,2,3,4,...
double k = 1; //第n项值
int a = 1; //分母阶乘的值
while (k > 1e-6){
e += k;
a *= n;
k = 1/(double)a;
n++;
}
return e;
}
Go实现
func eValue() float64 {
var n float64 = 1
var e float64 = 0
s:= 1
k := 1
for n > 1e-6 {
n = 1/float64(k)
e += n
k *= s
s++
}
return e
}