k=1 から始まり k=100 で終わるこのアルゴリズムを計算するプログラムを作成しました。
作成したコードは次のとおりです。
public static void calculatePi() {
BigInteger firstFactorial;
BigInteger secondFactorial;
BigInteger firstMultiplication;
BigInteger firstExponent;
BigInteger secondExponent;
int firstNumber = 1103;
BigInteger firstAddition;
double summationPi = 3.0;
double currentPi = 3.0;
double pi = 3.0;
int secondNumber = 2;
double thirdNumber = Math.sqrt(2.0);
int fourthNumber = 9801;
double prefix = 1;
for(int i=1;i<101;i++){
firstFactorial = factorial(4*i);
secondFactorial = factorial(i);
firstMultiplication = BigInteger.valueOf(26390*i);
firstExponent = exponent(secondFactorial, 4);
secondExponent = exponent(BigInteger.valueOf(396),4*i);
firstAddition = BigInteger.valueOf(firstNumber).add(firstMultiplication);
summationPi = firstFactorial.intValue()*firstAddition.intValue();
summationPi /= firstExponent.intValue()*secondExponent.intValue();
currentPi += summationPi;
}
prefix = secondNumber*thirdNumber;
prefix = prefix/fourthNumber;
summationPi = summationPi*prefix;
pi = 1/summationPi;
System.out.println("Pi is: " + pi);
return;
}
関数 exponent(a,b); a^b の結果を返します。関数 factorial(a) は、a の階乗を返します。これらの機能が両方とも完全に機能することを証明しました。しかし、このコードは不思議なことに "NaN" を返しているようです。何かがゼロで割られるときにこれが起こることは理解していますが、何かがゼロで割られるポイントを見つけることができませんでした。これを引き起こす他の何かがありますか/私は間違っていますか?
注: for ステートメントでは、アルゴリズムで i を k として使用しています。
前もって感謝します!