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 powint x int n
if n return ;
find x to the n recursively
int t powx n ;
if n is even, the result is t squared
if n is odd, the result is t squared times x
if n
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
