Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static BigInteger power(BigInteger x, BigInteger n) {
- if (n.equals(BigInteger.ZERO)) {
- return BigInteger.ONE;
- }
- if (n.equals(BigInteger.ONE)) {
- return x;
- }
- BigInteger temp = power(x, n.divide(bigTwo));
- if (n.remainder(bigTwo).compareTo(BigInteger.ZERO) > 0) {
- return x.multiply(temp);
- }
- else {
- if(n.compareTo(BigInteger.ZERO)>0) {
- return x.multiply(temp.multiply(temp));
- }
- else {
- return (temp.multiply(temp)).divide(x);
- }
- }
- }
Advertisement