I have this code to find the largest prime factor for the number 600851475143
:
BigInteger tal = new BigInteger("600851475143");
BigInteger tempBiggest = new BigInteger("2");
BigInteger temp = new BigInteger("2");
boolean check = true;
for (BigInteger I = new BigInteger("2"); I.compareTo(tal) < 0; I = I.add(BigInteger.ONE)) {
if (tal.mod(I).equals(BigInteger.ZERO)) {
temp = I;
if (temp.mod(new BigInteger("2")).equals(BigInteger.ZERO)) {
check = false;
} else {
for (BigInteger t = new BigInteger("2"); t.compareTo(temp) < 0; t = t.add(BigInteger.ONE)) {
if (temp.mod(t).equals(BigInteger.ZERO)) {
check = false;
break;
}
}
if (check) {
tempBiggest = I;
}
check = true;
}
}
}
System.out.println(tempBiggest);
System.exit(0);
The code works for smaller numbers, but not for this large one. Is there a way to simplify this or is my entire code wrongly built?