Anyone have an idea on how to implement such a problem in java ?
"Implement a subroutine that takes three positive integer arguments (a; b; n) and returns the value of ( (a to the power of b) mod n), where the arguments are represented by about 100 decimal digits. Using four different methods."
Thanks in advance
UPD : Methods were be as following
M1)
public BigInteger Result1(BigInteger a , BigInteger b , BigInteger n){
BigInteger Res = new BigInteger("1");
for (BigInteger i = new BigInteger("0"); !i.equals(b); i = i.add(new BigInteger("1"))) {
Res = Res.multiply(a).mod(n);
}
return Res;
}
M2)
public BigInteger Result2(BigInteger a , BigInteger b , BigInteger n){
BigInteger Res = new BigInteger("1");
for (BigInteger i = new BigInteger("0"); !i.equals(b); i = i.add(new BigInteger("1"))) {
Res = Res.multiply(a);
}
return Res.mod(n);
}
M3)
ublic BigInteger Result3(BigInteger a , BigInteger b , BigInteger n){
if(b.equals(new BigInteger("0"))){
return new BigInteger("1");
}
else if(b.mod(new BigInteger("2")).equals(new BigInteger("0"))){
BigInteger Res = Result3(a,b.divide(new BigInteger("2")),n);
return (Res.multiply(Res)).mod(n);
}
else{
return ( (a.mod(n)).multiply(Result3(a, b.subtract(new BigInteger("1")), n)) ).mod(n);
}
}
M4)
public BigInteger Result4(BigInteger a , BigInteger b , BigInteger n){
BigInteger Res = new BigInteger("1");
while(!b.equals(new BigInteger("0"))) {
if(!(b.mod(new BigInteger("2"))).equals(new BigInteger("0"))) {
Res = Res.multiply(a).mod(n);
}
a = a.multiply(a).mod(n);
b = b.divide(new BigInteger("2"));
}
return Res;
}
Flustered this was put on hold...
For the sake of theory, if you wanted to write your own custom method please check the following out, based on a math trick to avoid the computation. First the solution, and then the math behind it.
Your subroutine could look something like the following:
The following is an example of how you could call the method:
Now for the explanation
I'm doing smaller numbers for the sake of explaining things... it's the process by hand that get's turned into the code at the end.
a
= 17b
= 63n
= 5First you want to see what base2 powers your power number
b
is composed of. For example:OR in binary 11111
This can be found programatically by iterating from 0 to our BigInteger b's
.bitLength()
and and checking with.testBit()
the given bits. Hence the for-loop in the Java code.Next we find multiples of your base modulus (
n
) target (you need to go as far out in value as your highest power of two from the previous segment):let us call each simplified value
a<power_of_2>
.and the by hand values:
Problematically this can be done in sync with the previous step of finding the base2 powers, each iteration you also find the newest
a<power_of_2>
. Each additional square needs to be calculated until we've hit the max value, hence the ever increasingprevious
value in the loop.Lastly, we take our neccessary a values and multiple them by each other, followed by modulus of our
n
. this is within the if statement of the foor-loop.and from the above step we have take the values and multiple them together (followed by the modulus at the end of the loop):