title: Fastest algorithm to find if a BigInteger is a prime number or not?
body: <p>I am writing a method that detects if a BigInteger is prime or not. I have used the following code/algorithm to check if a given number is prime or not. But this is extremely slow and takes long time if a number is lets say 10 digits long. </p>
  
  <pre><code> 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&gt;2){
              return false;
              }
  
              }       
  
          } 
      return true;
  
      }
  </code></pre>
  
  <p>Is there any better algorithms to work with BigInteger prime number? I could not find a question related to this in Stackoverflow. If you have came across such question please let me know or if you have an idea on how to solve then your ideas are much appreciated.</p>