N桁を含むフィボナッチ数列の最初の数字を見つけようとしています(Nは500から2000の範囲のどこかにあります)。私は次のコードでこれを行おうとしています:
BigInteger num = BigInteger.valueOf(2);
BigInteger num1 = BigInteger.ONE;
BigInteger num2 = BigInteger.ONE;
int record = 0;
BigInteger TEN = BigInteger.valueOf(10);
public BigInteger run()
{
BigInteger temp = BigInteger.ZERO;
while(num2.compareTo(TEN.pow(SomeN - 1)) < 0)
{
if(num2.compareTo(TEN.pow(record + 1)) >= 0)
{
System.out.println(""+record);
record++;
}
temp = num1.add(num2);
num1 = num2;
num2 = temp;
num = num.add(BigInteger.ONE);
}
System.out.println(""+num);
System.out.println(""+num2);
return num2;
}
問題は、1500桁をテストすると、得られる答えが明らかに間違っていることです。答えが何であるかわかりません。アルゴリズムが10の累乗でオフになっている場合(つまり、1499桁と1501をチェックした場合)、すぐに答えをチェックしましたが、役に立ちませんでした。誰かが何が悪いのかわかりますか?