如何获得BigInteger的准确长度?

时间:2015-02-19 11:49:52

标签: java biginteger fibonacci

我正在尝试创建一个程序,它将在Fibonacci序列中生成数字,直到找到序列中的1,000位数字。我使用的代码运行正常,并提供有效的输出,但是,我无法检索每个数字的长度;使用BigInteger我已将BigInteger转换为String并使用String.length()方法获取长度,但是,我发现这并未给出实际长度,我不明白为什么。

import java.util.ArrayList;
import java.math.BigInteger;
public class problemTwentyFive {
  public static void main(String [] args) {
     ArrayList<BigInteger> fibonacciNumbers = new ArrayList<BigInteger>();
     boolean validNo = true;
     int x = 2;
     BigInteger tempAns = new BigInteger(""+0);
     fibonacciNumbers.add(new BigInteger(""+x));
     fibonacciNumbers.add(new BigInteger(""+x));
     do {
      tempAns = fibonacciNumbers.get(x-1).add(fibonacciNumbers.get(x-2));
      if (tempAns.toString().length() <= 1000) {
         System.out.println(tempAns.toString().length());
         if(tempAns.toString().length() == 1000) {
            fibonacciNumbers.add(tempAns);
            validNo = false;
            break;
        } else {
           fibonacciNumbers.add(tempAns);
        }
      }
      x++;
      if (tempAns.toString().length() > 1000) {
         validNo = false;
         break;
      }
      System.out.println(tempAns);
   } while (validNo == true);
   System.out.println("The first term in the Fibonacci sequence to contain 1,000 digits is term: " + fibonacciNumbers.size());
  }
}

有没有更好的方法来获得BigInteger的长度?我已经读到了BigInteger: count the number of decimal digits in a scalable method

这个问题

更新 运行程序后输出的文本文本是:

The first term in the Fibonacci sequence to contain 1,000 digits is term: 4781

我们知道这是错误的,因为如果我们看一下我正在尝试的项目,当我们输入4781作为答案时,它是不正确的。点击here查看项目(项目Euler - 问题25

3 个答案:

答案 0 :(得分:2)

执行此代码时(即找到解决方案):

            if ((tempAns.toString().length()) == 1000)
            {
                fibonacciNumbers.add(tempAns);
                validNo = false;
                break;
            }

不打印tempAns。这就是为什么你最后打印的数字只有999位数。

如果您在最后添加System.out.println("and is: " + tempAns);,则在主方法结束之前,您将获得所需的数字。因此答案是4781 + 1 = 4782

答案 1 :(得分:1)

我认为真正的问题是你开始使用值为2,2的系列,但它应该是1,1,...

下面:

 int x = 2;
 BigInteger tempAns = new BigInteger(""+0);
 fibonacciNumbers.add(new BigInteger(""+x));
 fibonacciNumbers.add(new BigInteger(""+x));

应该是:

 int x = 2;
 BigInteger tempAns = new BigInteger(""+0);
 fibonacciNumbers.add(new BigInteger(""+1));
 fibonacciNumbers.add(new BigInteger(""+1));

这使得你的系列在一个学期之后达到1000长度值,为你提供Bukhard给你的真实答案(我认为错误的原因,因为你正在添加长度为1000的值)。

答案 2 :(得分:0)

代码看起来比复制代码和许多分支要复杂得多。例如,您测试(... <= 1000)(... = 1000),然后再测试(... > 1000)。逻辑并不容易理解。我保留了你的算法,但只是消除了所有这些分支测试的噪音:

public static void main(String [] args) {
        ArrayList<BigInteger> fibonacciNumbers = new ArrayList<BigInteger>();
        boolean validNo = true;
        int x = 2;
        BigInteger tempAns = null;
        fibonacciNumbers.add(BigInteger.valueOf(1));
        fibonacciNumbers.add(BigInteger.valueOf(1));
        do {
            tempAns = fibonacciNumbers.get(x-1).add(fibonacciNumbers.get(x-2));
            fibonacciNumbers.add(tempAns);
            x++;
            System.out.println("x=" + x + ", length=" + tempAns.toString().length());
            if(tempAns.toString().length() >= 1000) {
                validNo = false;
            }
        } while (validNo == true);
        System.out.println("The first term in the Fibonacci sequence to contain 1,000 digits is term: " + fibonacciNumbers.size());
    }