第六章第十四题(估算PI)(Estimate PI)

第六章第十四题(估算PI)(Estimate PI)

  • *6.14(估算PI)PI可以使用下面的数列进行计算:
    在这里插入图片描述
    编写一个方法,对于给定的i返回m(i),并且编写一个测试程序,显示如下表格:
    i m(i)
    1 4.0000
    101 3.1515
    201 3.1466
    301 3.1449
    401 3.1441
    501 3.1436
    601 3.1433
    701 3.1430
    801 3.1428
    901 3.1427
    *6.14(Estimate PI)PI can be computed using the following summation:
    在这里插入图片描述
    Write a method that returns m(i) for a given i and write a test
    program that displays the following table:
    i m(i)
    1 4.0000
    101 3.1515
    201 3.1466
    301 3.1449
    401 3.1441
    501 3.1436
    601 3.1433
    701 3.1430
    801 3.1428
    901 3.1427
  • 参考代码:
package chapter06;

public class Code_14 {
    public static void main(String[] args) {
        printTableHead();
        for (int i = 0; i <= 9; i++) {
            System.out.printf("%d\t%.4f\n",i * 100 + 1,ComputePI(i * 100 + 1));
        }
    }
    public static double ComputePI(int i) {
        double pi = 0;
        for (int j = 1; j <= i; j++)
            pi += Math.pow(-1, j + 1) / (2 * j - 1);
        return pi * 4;
    }
    public static void printTableHead() {
        System.out.println("i\tm(i)");
    }
}

  • 结果显示:
i	m(i)
1	4.0000
101	3.1515
201	3.1466
301	3.1449
401	3.1441
501	3.1436
601	3.1433
701	3.1430
801	3.1428
901	3.1427

Process finished with exit code 0

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值