当前位置: 首页 > 面试题库 >

BigInteger:以可扩展方法计算小数位数

澹台聪
2023-03-14
问题内容

我需要计算的小数位数BigInteger。例如:

  • 99 退货 2
  • 1234 退货 4
  • 9999 退货 4
  • 12345678901234567890 退货 20

我需要做的这 一个BigInteger184948十进制数字多我该如何快速且可扩展?

转换到字符串的 方法是缓慢的:

public String getWritableNumber(BigInteger number) {
   // Takes over 30 seconds for 184948 decimal digits
   return "10^" + (number.toString().length() - 1);
}

这种 按十个循环进行循环的 方法甚至更慢:

public String getWritableNumber(BigInteger number) {
    int digitSize = 0;
    while (!number.equals(BigInteger.ZERO)) {
        number = number.divide(BigInteger.TEN);
        digitSize++;
    }
    return "10^" + (digitSize - 1);
}

有没有更快的方法?


问题答案:

这看起来像在工作。我还没有进行详尽的测试,也没有进行任何时间的测试,但似乎运行时间合理。

public class Test {
  /**
   * Optimised for huge numbers.
   *
   * http://en.wikipedia.org/wiki/Logarithm#Change_of_base
   *
   * States that log[b](x) = log[k](x)/log[k](b)
   *
   * We can get log[2](x) as the bitCount of the number so what we need is
   * essentially bitCount/log[2](10). Sadly that will lead to inaccuracies so
   * here I will attempt an iterative process that should achieve accuracy.
   *
   * log[2](10) = 3.32192809488736234787 so if I divide by 10^(bitCount/4) we
   * should not go too far. In fact repeating that process while adding (bitCount/4)
   * to the running count of the digits will end up with an accurate figure
   * given some twiddling at the end.
   * 
   * So here's the scheme:
   * 
   * While there are more than 4 bits in the number
   *   Divide by 10^(bits/4)
   *   Increase digit count by (bits/4)
   * 
   * Fiddle around to accommodate the remaining digit - if there is one.
   * 
   * Essentially - each time around the loop we remove a number of decimal 
   * digits (by dividing by 10^n) keeping a count of how many we've removed.
   * 
   * The number of digits we remove is estimated from the number of bits in the 
   * number (i.e. log[2](x) / 4). The perfect figure for the reduction would be
   * log[2](x) / 3.3219... so dividing by 4 is a good under-estimate. We 
   * don't go too far but it does mean we have to repeat it just a few times.
   */
  private int log10(BigInteger huge) {
    int digits = 0;
    int bits = huge.bitLength();
    // Serious reductions.
    while (bits > 4) {
      // 4 > log[2](10) so we should not reduce it too far.
      int reduce = bits / 4;
      // Divide by 10^reduce
      huge = huge.divide(BigInteger.TEN.pow(reduce));
      // Removed that many decimal digits.
      digits += reduce;
      // Recalculate bitLength
      bits = huge.bitLength();
    }
    // Now 4 bits or less - add 1 if necessary.
    if ( huge.intValue() > 9 ) {
      digits += 1;
    }
    return digits;
  }

  // Random tests.
  Random rnd = new Random();
  // Limit the bit length.
  int maxBits = BigInteger.TEN.pow(200000).bitLength();

  public void test() {
    // 100 tests.
    for (int i = 1; i <= 100; i++) {
      BigInteger huge = new BigInteger((int)(Math.random() * maxBits), rnd);
      // Note start time.
      long start = System.currentTimeMillis();
      // Do my method.
      int myLength = log10(huge);
      // Record my result.
      System.out.println("Digits: " + myLength+ " Took: " + (System.currentTimeMillis() - start));
      // Check the result.
      int trueLength = huge.toString().length() - 1;
      if (trueLength != myLength) {
        System.out.println("WRONG!! " + (myLength - trueLength));
      }
    }
  }

  public static void main(String args[]) {
    new Test().test();
  }

}

在我的Celeron M笔记本电脑上花了大约3秒钟,因此它在某些不错的工具包上应该不到2秒钟。



 类似资料:
  • 我需要一个大整数的小数位数。例如: 返回 返回 返回 返回 我需要对和十进制数字及更多数字执行此操作。我如何快速且可扩展地完成这项工作? 转换为字符串的方法很慢: 这种10倍循环的方法更慢: 有没有更快的方法?

  • 我正在尝试在旁边使用值方法。 不幸的是,编译器说不兼容的类型。 如果我将s更改为s,它仍然不喜欢它。

  • 我正在学习浮点格式(IEEE)。在单精度浮点格式中,提到尾数有24位,因此它具有6 1/2十进制数字的精度(根据书中“理解机器”),以及7.22十进制数字的精度。 我不明白精度的小数位数是怎么算出来的。有人能告诉我吗?

  • 问题内容: 我编写了一个小程序来用Java计算Pi。 我的总结目前正在输入中。 我有两个输出,一个是Pi到16个小数位(原始输出)。第二个输出使用/ 舍入到3-5个小数位。 我花了一段时间浏览和浏览Stackoverflow,但是作为一个初学者,我不确定找到所需的正确方法所需要的任何内容,所以请原谅我。 是否可以计算出超过16个小数位的答案(即Pi)?说32岁?如果是这样,我该如何处理? 我已经看

  • 问题内容: 用MySQL计算中位数的最简单方法(希望不是太慢)是什么?我一直在寻找均值,但是我很难找到一种简单的计算中位数的方法。现在,我将所有行返回给PHP,进行排序,然后选择中间行,但是肯定必须有一个简单的方法可以在单个MySQL查询中完成。 示例数据: 排序给出,因此中位数应为,而其中== 。 问题答案: 在MariaDB / MySQL中: 史蒂夫·科恩(Steve Cohen)指出,在第

  • 问题内容: 说我有一个像这样的课程: 它具有一个带有扩展它的类的层次结构: 然后在其他地方,我有一堂课利用了这些东西: 我是否可以指定A的子类,以便使用更具体的“某物”类覆盖setSomething方法?这就是我想要做的: 目前,我正在A类中执行以下操作: 如果SuperSomething的类型不适合这些类,则B类和C类在checkClass方法中引发异常。 编辑:我已经尝试使用上述确切的方法签名