Java中的递归Chudnovsky算法
我是一名计算机工程专业的学生,我有一个项目要做,就是做一个Chudnovsky算法来计算π,但我的问题是要做一个十进制的小数点,也就是说,如果a的长度是3,它将是3.14,我已经完成了代码,得到了3.141592653589734,但是我不知道如何使用递归方法一点一点地完成Java中的递归Chudnovsky算法,java,algorithm,recursion,Java,Algorithm,Recursion,我是一名计算机工程专业的学生,我有一个项目要做,就是做一个Chudnovsky算法来计算π,但我的问题是要做一个十进制的小数点,也就是说,如果a的长度是3,它将是3.14,我已经完成了代码,得到了3.141592653589734,但是我不知道如何使用递归方法一点一点地完成 //This class implements an interface which only contains the method calcularPi public class Chudnovsky_Impleme
//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));
}
如果有什么是有点模糊或你不太明白英语不是我的主要语言抱歉
如果您想对您使用的相同逻辑使用递归,那么您应该更改
else {
double resultado= (Pi(k))+(Pi(k-1));
return resultado;
}
到
这也是在阶乘方法中使用的方法。使用递归:
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 );
}
}
}
请注意,此答案仅在k=17之前有效,并且存在精度问题!
如果需要更多的数字或更高的精度,则需要使用BigDecimal。递归只意味着函数调用自身:看看阶乘函数如何调用阶乘n-1,也许可以在calculatePi中执行类似的操作……你确定分子和分母都正确吗?我在维基上检查了算法,我不确定你是否写了每一个因子。。。另外,如果你愿意,你可以在这里用葡萄牙语问:但我会在这里看看你的问题。。。还有一件事:你还有其他的代码吗?或者都在这里?我添加了一张老师给你的图片us@LuisOrtiz我正在努力。。。请稍等一下……是的,我刚才这么做了,但问题是,我需要按照我所说的,用一定数量的数字给出这个数字,我相信会继续给我这个数字,但会有很多数字decimals@LuisOrtiz你没有提到你需要在结果中设置什么数字限制。在这种情况下,可能会检查位数,如果达到位数则返回,否则返回舍入。问题是我必须这样做,这是问题的复杂部分problem@LuisOrtiz请检查这个新版本。
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