第五章第二十六题(计算e)(Compute e)

第五章第二十六题(计算e)(Compute e)

  • **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;

public class Code_26 {
    public static void main(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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值