1

For some reason my val variable isn't increasing within the while-loop. Any reason as to why it's not doing so? val.add(BigInteger.ONE);

import java.math.*;

public static void main(String[] args) {

    /* Variables */
    BigInteger PQ = new BigInteger("17");
    BigInteger E = new BigInteger("7");

    BigInteger val = new BigInteger("2");

    /* Find the PRIME factors of PQ */
    while (PQ.compareTo(BigInteger.ONE) == 1) { // while the first value is greater
        if (PQ.mod(val).equals(BigInteger.ZERO)) {
            System.out.print(val + "  ");
        } else {
            val.add(BigInteger.ONE);
        }
    }
}
Dandy
  • 25
  • 2
  • 1
    Although [`BigInteger.compareTo(BigInteger val)`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#compareTo-java.math.BigInteger-) explicitly lists return value as *"-1, 0 or 1"*, it is better to code to the more general contract inherited from `Comparable` which says *"a negative integer, zero, or a positive integer"*, so you should write `a.compareTo(b) > 0` instead of `a.compareTo(b) == 1`. It also reads better, since it means `a > b`, i.e. `a.compareTo(b) OP 0` means `a OP b`, where `OP` can be `<`, `<=`, `==`, `>=`, `>`, or `!=`. – Andreas Sep 11 '16 at 04:31

2 Answers2

7

val.add(BigInteger.ONE) returns a new value. You do not assign it to anything:

val = val.add(BigInteger.ONE);

Class BigInteger

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
0

You can initialize the sum again.

BigInteger val=BigInteger.valueOf(10);
BigInteger val2=BigInteger.valueOf(20);
BigInteger val3=va1.add(val2);