格雷戈里级数给出的结果值很接近,但不正确?

问题:

数字“pi”(3.14159...)不能表示为两个数字的简单比率。相反,pi 的值通常是通过对无限数列的项求和来计算的。随着该系列中越来越多的项被评估,总和接近 pi 的“真实”值。可用于此目的的一个级数称为格里高利级数,它计算 pi 的值如下: pi = 4/1 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + ……

观察到随着计算的进行,级数项的数值变得越来越小。例如,第一项的值为 4/1 = 4,第二项的值为 4/3 = 1.333…,第三项的值为 4/5 = 0.8,依此类推。

编写一个程序,使用格里高利级数计算 pi 的值。程序的输入将是一个称为 limit 的十进制值。程序应通过对级数的项求和来计算 pi 的值,但仅直到被求和的项的值小于或等于 limit 值为止,此时程序终止总结。因此,添加到总和中的级数的最后一项是其值小于或等于 limit 值的第一项。

然后,程序打印出此时 pi 的计算值,以及通过计算求和的实际项数。

import java.util.Scanner;


public class Main {


    public static void main(String[] args) {


        Scanner kb=new Scanner(System.in);

        System.out.println("Enter Limit");

        double limit=kb.nextDouble();


        int TermsSum=0;

        double PiVal=0;

        double min=1;

        double PiCon = (4.0 / min);

        while (limit<=PiCon) {

            if (limit <= PiCon) {

                TermsSum++;

                PiVal += (PiCon);

                min += 2;

                PiCon = (4.0 / min);

            }

            if (limit <= PiCon) {

                TermsSum++;

                PiVal += (-(PiCon));

                min += 2;

                PiCon = (4.0 / min);

            }

        }



        System.out.println("Limit: "+limit);

        System.out.println("Pi Value: "+PiVal);

        System.out.println("Terms Summed: "+TermsSum);

    }

}

如果我输入以下限制,预期结果如下


Limit: 0.075 = Pi: 3.1058897382719475 = Terms Summed: 28

Limit: 0.00001 = Pi: 3.141597653564762 = Terms Summed: 200001

我当前计划的结果是


Limit: 0.075 = Pi: 3.1786170109992202 = Terms Summed: 27

Limit: 0.00001 = Pi: 3.1415876535897618 = Terms Summed: 200000


幕布斯7119047
浏览 117回答 1
1回答

繁星coding

您对迭代次数的计算是错误的。我已经更正了你的代码。为了方便起见,我还打印了该系列。现在该值符合预期。&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner kb=new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Limit");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double limit=kb.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int TermsSum=1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double min=1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double PiVal= 4.0 / min;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double PiCon = 4.0 / min;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("4.0 /" + min);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (limit<=PiCon) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (limit <= PiCon) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TermsSum++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min += 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PiCon = (4.0 / min);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PiVal += (-(PiCon));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("-"+"4.0 /" + min);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (limit <= PiCon) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TermsSum++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min += 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PiCon = (4.0 / min);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PiVal += (PiCon);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("+"+"4.0 /" + min);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Limit: "+limit);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Pi Value: "+PiVal);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Terms Summed: "+TermsSum);&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java