Sparrow
Sparrow

Reputation: 305

BigInteger loop executing infinitely

while(true) {
    if(((d.multiply(e)).mod(phi1)).equals(BigInteger.ONE))
        break;
    d.add(BigInteger.ONE);
}

I have the following code in my program, which means that

while(true) {
    if((d*e)%phil==1) 
        break;
    d++;
}

Here, e=17, phil=12816 and d=1 initially.

But even after waiting for a long time, the loop is still executing. What could be the mistake?

Upvotes: 1

Views: 83

Answers (1)

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

BigInteger is immutable and all operations on it return a new instance instead of modifying the current one. Thus d.add(BigInteger.ONE); does not change the value of d.

To fix the issue write: d = d.add(BigInteger.ONE);

Upvotes: 6

Related Questions