java - 使用 while 循环和递归方法计算 Pi (Java)

标签 java recursion while-loop pi

我的作业是使用他在类里面给我们的算法计算 Pi,确定正确的数字,并使用 while 循环和递归方法将 Pi 估计为六位数。但是我的“ super 聪明的教授”并没有告诉我们有关递归方法的任何该死的事情,当我给他发电子邮件时,他对我生气,因为我没有通过查看来理解它。到目前为止,这是我的代码,我省略了 while 循环方法和递归方法,因为我不知道如何执行这些操作。

public static final double REAL_PI = 3.14159;//PI is the value prof gave us on the handout
public static double Pi = 0; //Pi is the value of Pi that this program calculates
public static int m = 0; 

public static void main (String [] args)
{
    Algorithm(); //calls on method of calculating pi
    System.out.println("Calculated pi: " + Pi); //prints out pi
    countDigits(Pi); //calls on countdigits method
    System.out.println("Number of digits: " + c); //has the computer print out the count because that's how many digits are the same
    PiRecur(); //calls on estimate digits method
}

public static double Algorithm() //should return a double (pi)
{
    for(m=1; m<=100000; m++)
    {
        Pi += 4*(Math.pow(-1, m-1)/((2*m)-1));//Math.pow uses math package to calculate a power to use the algorithm
    }
    return Pi;
}

public static int countDigits (double Pi)
{
    int a = (int) Pi; //the int cast makes Pi and REAL_PI into integers so the program can compare each digit separately 
    int b = (int) REAL_PI;
    int c = 0;
    int count = 0;
    while(a == b)//if m less then or equal to 100,000 then while loop runs
    {
        count ++;
        a = (int) (Pi*(Math.pow(10,count))); //if a=b then the computer will multiply Pi and REAL_PI by 10 
        b = (int) (REAL_PI*(Math.pow(10,count)));
        /*when you input a and b 
         * while loop compares them
         * if a = b then loop continues until a doesn't equal b and loop ends
         */
    }
    c = count; //gives c the value of the count so it can be used outside the method
    return count;
}

}

最佳答案

我不确定使用 while 循环和递归的解决方案如何循环,因为我无法读懂你教授的想法,但我可以想到两种不同的解决方案,它们使用一个或其他。

使用while循环:

您不会运行算法任意次数的迭代(在您的示例中为 100000 次)并希望您足够接近预期结果。您使用 while 循环,并在每次迭代时检查当前的 Pi 计算结果是否足够接近目标。

public static double Algorithm()
{
    int m = 1;
    double Pi = 0.0;
    while (countDigits(Pi) < 6) {
        Pi += 4*(Math.pow(-1, m-1)/((2*m)-1)); // I'm assuming this formula works
        m++;
    }
    return Pi;
}

使用递归:

同样的解决方案可以转化为递归。这次,您向 Algorithm 提供初始索引 m (1) 和初始值 Pi (0)。该方法将第 m 项添加到 Pi 中。如果 Pi 的新值不够好(由 countDigits 确定),您可以进行递归调用,添加第 m+1项到 Pi 并再次检查新值是否足够好。当Pi的值精确到6位数字时,递归将停止。

public static double Algorithm(int m,double Pi)
{
    Pi += 4*(Math.pow(-1, m-1)/((2*m)-1));
    if (countDigits(Pi) < 6)
        Pi += Algorithm(m+1,Pi);

    return Pi;
}

您可以使用以下方式调用该方法:

Algorithm (1,0.0);

关于java - 使用 while 循环和递归方法计算 Pi (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285297/

相关文章:

java - dropwizard 应用程序中的配置更改以与 React browserHistory 配合使用

java - Spring4中从Service访问Repository时出现NullPointerException

C++:这是递归删除链表的正确方法吗?

javascript - while循环中非​​法的break语句

java - 如何知道表情符号的ASCII/UTF编码?

java - 库没有JAVADOC代码完整求助

java 使用递归查找字符串中的子字符串

c - 如何在C中实现多分支树结构

C while 循环部分执行一次额外迭代

ios - 如果在数组中找不到任何内容,为什么此循环会导致崩溃