Java Algorithms How to - Get the prime numbers








Question

We would like to know how to get the prime numbers.

Answer

import java.math.BigInteger;
/* w w w  . j  a  v  a 2s .  co m*/
public class Main {
  public static void main(String[] args) {
    int i = 0;
    while (true) {
      if (i > 1000) {
        break;
      }
      if (i > 1) {
        if (new BigInteger(i+"").isProbablePrime(i / 2)) {
          System.out.println(i);
        }
      }
      i++;
    }
  }
}

The code above generates the following result.