I am writing a method that determines whether BigInteger is simple or not. I used the following code / algorithm to check if a given number is prime or not. But it is very slow and takes a lot of time if the number allows you to speak 10 digits.
public boolean returnPrime(BigInteger testNumber){ int divisorCounter=1; BigInteger index,i ; for ( index= new BigInteger("2"); index.compareTo(testNumber) !=1; index=index.add(new BigInteger("1"))){ System.out.println(index); for(i= new BigInteger("2"); i.compareTo(index) != 1; i=i.add(new BigInteger("1"))){ if((testNumber.mod(i).equals(BigInteger.ZERO) )){ divisorCounter++; } if(divisorCounter>2){ return false; } } } return true; }
Are there any better BigInteger prime algorithms? I could not find a question related to this in Stackoverflow. If you are faced with such a question, please let me know or you have an idea on how to solve, then your ideas are greatly appreciated.
source share