Question

I have the following "while" loop in my Java code. All the variables in my code are BigIntegers.

BigInteger d=new BigInteger("1");
BigInteger e=new BigInteger("3");
BigInteger phi1=new BigInteger("6336");
while(true)
{
    if(((d.multiply(e)).mod(phi1)).equals(BigInteger.ONE))
        break;
    d=d.add(BigInteger.ONE);
    //System.out.println(d);
}

The loop is getting executed infinitely in this case. But I want to add a break on the following condition:

if((d*e)%phi1==1)
    break;

which I have converted into BigInteger as

if(((d.multiply(e)).mod(phi1)).equals(BigInteger.ONE))
    break;

Where am I getting it wrong? Can anyone tell why the loop is executing infinitely?

Was it helpful?

Solution

6336 is a multiple of 3, hence the condition that you're attempting to break upon would never be true.

Infinite loop is inevitable.

OTHER TIPS

You are dividing 3*d by 6336, and checking the remainder r. Let k be the division result:

3d = k*6336 + r
=> r = 3*(d - 2112*k)

Which means that the remainder is always a multiple of 3, therefore it can never be 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top