Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdded FastPower #731
Added FastPower #731
Conversation
Added FastPower
This looks like the same functionality as |
public class FastPowerTest { | ||
|
||
@Test | ||
public void test() { |
BigInteger ans = BigInteger.ONE; | ||
while (!k.equals(BigInteger.ZERO)) { | ||
int odd = k.mod(new BigInteger("2")).compareTo(BigInteger.ZERO); | ||
if(odd == 1){ |
havanagrawal
May 2, 2019
The result of compareTo
should only be compared with 0, there is no guarantee that it will be 1, only that it may be negative, 0 or positive.
if(odd == 1){ | ||
ans = ans.multiply(n).mod(mod); | ||
} | ||
k = k.divide(new BigInteger("2")); |
havanagrawal
May 2, 2019
Instead of using new BigInteger("2")
in a loop, we could declare it as a constant outside:
public static final BigInteger TWO = new BigInteger("2");
Thanks for your review @havanagrawal , I have fixed these problems. |
Added FastPower
We may calculate power with loops, but what if the index is too large ?
FastPower aims to calculate quickly in this circumstances with time complexity O(log k), where k is the index.