Java中的递归Chudnovsky算法

时间:2017-09-12 01:08:38

标签: java algorithm recursion

我是一名计算机工程专业的学生,​​我有一个项目要做Chudnovsky算法来计算Pi,但我得到的问题是十进制小数(i)意思是如果得到一个长度为3的它将是3.14),我已经完成了代码并且得到3.141592653589734但是我不知道如何做到这一点使用递归方法的位。到目前为止我得到的代码是

 //This class implements an interface which only contains the method calcularPi
 public class Chudnovsky_Implements implements Chudnovsky {


public  double calcularPi(int k)//This is where I'm trying to do it bit by bit which I'm probably doing it wrong.
{       
    if(k==0)
        return Pi(k);
    else {
    double resultado= (Pi(k))+(Pi(k-1));
    return resultado;

    }

}

public double Pi(int k)//Here i calculated the number Pi with a constant k that the user give(k is supposedly to be the number of digits)
{
    double numerador=(factorial(6*k)*((545140134*k)+13591409));
    double denominador =(factorial(3*k)*Math.pow(factorial(k), 3)*Math.pow(-640320, (3*k)));
    double Pi=(numerador/denominador);
    return Pi;
}

 public double factorial(int n)// This is a class to calculate an factorial of a number
 {
    if (n==0)
       return 1;
    else
       return n*(factorial(n-1));
}

如果有些含糊不清或者您不太懂英语不是我的主要语言抱歉

This is the integer the teacher gave to us

2 个答案:

答案 0 :(得分:1)

使用递归:

package q46166389;

public class Chudnovsky {

    public static void main( String[ ] args ) {
        int k = 13;

        final String outputFormat = "%." + ( k - 1 ) + "f";

        double result = new Chudnovsky( ).calculateLoop( k );

        // Format the output to the desired number of decimals
        System.out.println( "result = " + String.format( outputFormat, result ) );
        // Or just print it:
        System.out.println( "result = " + result );

        result = 1 / new Chudnovsky( ).calculateRecursive( k );

        System.out.println( "result = " + String.format( outputFormat, result ) );
        System.out.println( "result = " + result );
    }

    public double calculateLoop( int k ) {
        double result = 0;
        for ( int i = 0; i <= k; i++ ) {
            result = result + doCalc( i );
        }
        return 1 / result;
    }

    public double calculateRecursive( int k ) {
        if ( k == 0 ) { return doCalc( k ); }

        return doCalc( k ) + calculateRecursive( k - 1 );
    }

    public double doCalc( int k ) {
        double numerator = Math.pow( -1, k ) * factorial( 6 * k ) * ( 545140134 * k + 13591409 );
        double denominator = factorial( 3 * k ) * Math.pow( factorial( k ), 3 ) * Math.pow( 640320, 3 * k + 3.0 / 2.0 );
        return 12.0 * numerator / denominator;
    }

    public double factorial( int n ) {
        if ( n == 0 ) {
            return 1;
        } else {
            return n * factorial( n - 1 );
        }
    }

}

输出:

result = 3.141592653590
result = 3.1415926535897936
result = 3.141592653590
result = 3.1415926535897936

请注意,此答案仅适用于k = 17并且存在精度问题! 如果您需要更多数字或更高精度,则需要使用BigDecimal

答案 1 :(得分:0)

如果你想使用与你使用的逻辑相同的递归,那么你应该改变

else {
    double resultado= (Pi(k))+(Pi(k-1));
    return resultado;
}

else {
    double resultado= calcularPi(k)+ calcularPi(k-1); // calling the same method from within it
    return resultado;
}

也是factorial方法中使用的内容。