java prime numbers 1 to 1000000

Solutions on MaxInterview for java prime numbers 1 to 1000000 by the best coders in the world

showing results for - "java prime numbers 1 to 1000000"
Julián
16 Mar 2017
1public class Main {
2  public static void main(String[] args) {
3    for (int i = 2; i < 1_000_000; ++i) {
4      boolean isPrime = true;
5      int sqrt = (int)Math.ceil(Math.sqrt(i));
6      for (int divisor = 2; divisor <= sqrt; ++divisor) {
7        if (i % divisor == 0) {
8          isPrime = false;
9          break;
10        }
11      }
12      if (isPrime) {
13        System.out.println(i);
14      }
15    }
16  }
17}