Question

int SIZE = 512;        
p = new BigInteger(SIZE, 15, new Random());
q = new BigInteger(SIZE, 15, new Random());
r = new BigInteger(SIZE, 15, new Random());

n = p.multiply(q);
temp1=n;
n = n.multiply(r);
   if (temp1.multiply(r)!=n) System.out.println("test");

My code here prints test when it shouldn't. Why?

Was it helpful?

Solution

You have to use equals to compare Object equality.

!= or == compare references.

BigInteger b0 = new BigInteger("0");
BigInteger b1 = new BigInteger("0");
System.out.println(b0 != b1);
System.out.println(!b0.equals(b1));

Output

true
false

OTHER TIPS

According to this implementation:

public BigInteger multiply(BigInteger val) {
    if (val.signum == 0 || signum == 0)
        return ZERO;

    int[] result = multiplyToLen(mag, mag.length,
                                 val.mag, val.mag.length, null);
    result = trustedStripLeadingZeroInts(result);
    return new BigInteger(result, signum == val.signum ? 1 : -1);
}

temp1.multiply(r) returns a new BigInteger object, which will have a different address than n. Use !temp1.multiply(r).equals(n).

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