What complexity are operations on BigInteger?

前端 未结 4 1847
臣服心动
臣服心动 2020-12-09 12:42

What complexity are the methods multiply, divide and pow in BigInteger currently? There is no mention of the computationa

4条回答
  •  没有蜡笔的小新
    2020-12-09 13:15

    As noted in the comments on @Bozho's answer, Java 8 and onwards use more efficient algorithms to implement multiplication and division than the naive O(N^2) algorithms in Java 7 and earlier.

    Java 8 multiplication adaptively uses either the naive O(N^2) long multiplication algorithm, the Karatsuba algorithm or the 3 way Toom-Cook algorithm depending in the sizes of the numbers being multiplied. The latter are (respectively) O(N^1.58) and O(N^1.46).

    Java 8 division adaptively uses either Knuth's O(N^2) long division algorithm or the Burnikel-Ziegler algorithm. (According to the research paper, the latter is 2K(N) + O(NlogN) for a division of a 2N digit number by an N digit number, where K(N) is the Karatsuba multiplication time for two N-digit numbers.)

    Likewise some other operations have been optimized.


    There is no mention of the computational complexity in the documentation (nor anywhere else).

    Some details of the complexity are mentioned in the Java 8 source code. The reason that the javadocs do not mention complexity is that it is implementation specific, both in theory and in practice. (As illustrated by the fact that the complexity of some operations is significantly different between Java 7 and 8.)

提交回复
热议问题