The first results in 41 while the second results in -9 when the expected shud be 6.
Thats most probably happening due to overflow. To illustrate this simple fact try compiling and running the following code snippet:-
class OverflowDemo {
public static void main(String[] args) {
short a = 32767;
a++;
System.out.println("a = " + a);
}
}
The above chunk of code on compiling and executing would give the following output :-
stephen@steve:~/Development/java/daniweb> javac OverflowDemo.java
stephen@steve:~/Development/java/daniweb> java OverflowDemo
a = -32768
stephen@steve:~/Development/java/daniweb>
Here when we added 1 to "a", we exceeded the capacity of "short" and it rounded off to -32768 due to overflow, similarly Math.pow(41, 77) exceeded the capacity of int.
Try casting the value returned by it to "long" to see if it fits there. If long also throws an error then the best option would be to use the BigInteger class where you will not be faced with any restriction on the value it can hold.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
int is 32 bits so it can only hold whatever value 32 bits can hold. And since you want to know if it's positive or negative, there goes one bit that would've gone towards calculation. So calculate that and there's your limit.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
edit: was looking at the wrong code, nevermind
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Now considering the piece of code that you say causes the problem :-
while(true){
key = get_random(new BigInteger("100"));
if((key.multiply(E)).mod(P_min_1_into_Q_min_1).
equals(new BigInteger("1")))
break;
}
Now from what I see, what **I think** is happening is that your "if" condition is never getting satisfied as a result of which your program goes into an "infinite" loop giving you the feeling that it has hung up.
My suggestion is put a System.out.print statement just in front of your "if" with its contents as the "if condition" so that you can visualize what is actually the state of your variables or where your values are drifting off track.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154