**5.26(计算e)You can approximate e using the following summation:
编写程序,显示当i = 10000,20000,…,100000时e的值。 提示:由于,那么。将e和通项item初始化为1,反复将新的item加到e上。新的item由前一个item除以i得到,其中 i >= 2。 **5.26(Compute e) You can approximate e using the following summation:
Write a program that displays the e value for i = 10000, 20000, …, and 100000. (Hint: Because , then Initialize e and item to be 1, and keep adding a new item to e. The new item is the previous item divided by i, for i >= 2.)
参考代码:
package chapter05;publicclassCode_26{publicstaticvoidmain(String[] args){double e, item;
e = item =1;for(int i =1;i <=100000;i++){
item /= i;
e += item;if(i %10000==0)
System.out.println("The E value for i = "+ i +" is "+ e);}}}
结果显示:
The E value for i =10000 is 2.7182818284590455
The E value for i =20000 is 2.7182818284590455
The E value for i =30000 is 2.7182818284590455
The E value for i =40000 is 2.7182818284590455
The E value for i =50000 is 2.7182818284590455
The E value for i =60000 is 2.7182818284590455
The E value for i =70000 is 2.7182818284590455
The E value for i =80000 is 2.7182818284590455
The E value for i =90000 is 2.7182818284590455
The E value for i =100000 is 2.7182818284590455
Process finished with exit code 0