Re: Modulus with BigIntegers

Got it! Silly mistake. I should have used compareTo instead of ==. My current issue is that I need to use BigIntegers in the conditions of the for loop, is that not allowed? Because my method seems to get stuck in a loop when I try it.

Re: Modulus with BigIntegers

You'll have to convert everything to a BigInteger, then use the compareTo method.

However, I would strongly advise **against** writing a naive primality test for BigInteger numbers. There are much faster statistical methods for checking if a number is prime.

One of these is built into the BigInteger class: isProbablyPrime.

There is also another method I implemented here. This uses a "better" method for checking if a number is prime for twice the statistical accuracy. This is because this test always reports composite numbers as composite and prime numbers as probably prime, but the built-in method reports both probably prime and probably composite.