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

标签 java

问题:

The number “pi” (3.14159...) can not be expressed as a simple ratio of two numbers. Instead, the value of pi is typically calculated by summing up the terms of an infinite number series. As more and more terms in the series are evaluated, the sum approaches the “true” value of pi. One series that can be used for this purpose is called the Gregory series, which computes the value of pi as follows: pi = 4/1 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + …

Observe that the numeric values of the terms of the series get smaller and smaller as the calculation progresses. For example, the value of the 1st term is 4/1 = 4, the 2nd term is 4/3 = 1.333…, the 3rd term is 4/5 = 0.8, and so on.

Write a program that calculates the value of pi using the Gregory series. The input to the program will be a decimal value called limit. The program shall proceed to calculate the value of pi by summing up the terms of the series, but only until such time that the value of the term being summed becomes less than or equal to the value of limit, at which point the program terminates the summation. Thus, the last term of the series that is added to the sum is the first term whose value is less than or equal to the value of limit.

The program then prints out the calculated value of pi at that point, as well as the actual number of terms that were summed up by the calculation.

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

最佳答案

您对迭代次数的计算是错误的。我已经更正了你的代码。为了方便起见,我还打印了该系列。现在该值符合预期。

        public static void main(String[] args) {

            Scanner kb=new Scanner(System.in);
            System.out.println("Enter Limit");
            double limit=kb.nextDouble();

            int TermsSum=1;
            double min=1;
            double PiVal= 4.0 / min;
            double PiCon = 4.0 / min;
            System.out.print("4.0 /" + min);
            while (limit<=PiCon) {
                if (limit <= PiCon) {
                    TermsSum++;
                    min += 2;
                    PiCon = (4.0 / min);
                    PiVal += (-(PiCon));
                    System.out.print("-"+"4.0 /" + min);
                }
                if (limit <= PiCon) {
                    TermsSum++;
                    min += 2;
                    PiCon = (4.0 / min);
                    PiVal += (PiCon);
                    System.out.print("+"+"4.0 /" + min);
                }


            }
            System.out.println("Limit: "+limit);
            System.out.println("Pi Value: "+PiVal);
            System.out.println("Terms Summed: "+TermsSum);
        }

关于java - 格雷戈里级数给出的结果值很接近,但不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57935344/

相关文章:

java - 如何以编程方式将图像设置为墙纸?

java - 将对象数组从 servlet 发送到 JSP

java - 抛出异常而不中断方法的执行

java - SFTP 连接器配置

java - 如何从需要在 JAVA 中进行身份验证的 HTTP 代理后面的 Gmail 帐户发送电子邮件?

java - Android 在 4.4.2 samsung 上不释放内存

java - GWT 从小程序调用 JS 方法

java - Android 拉伸(stretch) TableLayout XML 中的项目

java - 混淆类型关系

java - Selenium 和 TestNG 中的参数化