What are complexities of Java 7’s methods pow and isProbablePrime in the BigInteger class?
I know that simple implementation of Rabin’s test is of O(k(log(n))^3) complexity and that can be reduced by incorporating the Schönhage-Strassen algorithm for the fast multiplication of long integers.
Assuming the standard algorithms, the complexities are:
where:
nis the number of digits in the operand.exponentis the exponent of the power function.M(n)is the run-time for ann x ndigit multiplication. Which I believe isO(n^2)as of Java 6.Explanation for
pow():For an input operand of n-digits long raised to a power of
exp, the output is roughlyn * expdigits long. This is done by binary-powering algorithm where the operand is squared at each iteration. So the complexity becomes:This is a geometric sum, so the sum becomes
O( M(n * exp) ).Explanation for
IsProbablePrime():For a fixed number of Rabin-Miller iterations, each iteration has
O(n)multiplications of sizen x ndigits. Therefore, the complexity becomesO( n * M(n) ).