public class EratosthenesSieve { public static final int MAX = 121; public static void main(String[] args) { boolean[] primes = new boolean[MAX]; eratosthenesSieve(primes); System.out.printf("Primes in range [2..%d] are: ", MAX); for (int i = 2; i < MAX; i++) { if(primes[i]) { System.out.printf(i + " "); } } } private static void eratosthenesSieve(boolean[] primes) { for (int i = 2; i < primes.length; i++) { primes[i] = true; } for (int i = 2; i < Math.sqrt(MAX); i++) { if (primes[i]) { for (int j = i * i; j < MAX; j += i) { primes[j] = false; } } } } }