Question: Many encryption algorithms depend on the ability to raise large integers to a power. Here is a method that implements an efficient algorithm for integer

Many encryption algorithms depend on the ability to raise large integers to a power. Here is a method that implements an efficient algorithm for integer exponentiation:
public static int pow(int x, int n)
{
if (n ==0) return 1;
// find x to the n/2 recursively
int t = pow(x, n /2);
// if n is even, the result is t squared
// if n is odd, the result is t squared times x
if (n %2==0){
return t * t;
} else {
return t * t * x;
}
}
The problem with this method is that it works only if the result is small enough to be represented by an int. Rewrite it so that the result is a BigInteger. The parameters should still be integers, though.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!