get Primes Below - Java java.lang

Java examples for java.lang:int prime

Description

get Primes Below

Demo Code


//package com.java2s;

import java.util.Collection;

public class Main {
    public static void getPrimesBelow(Collection<Integer> collection,
            int upperLimit) {
        eratosthenesSieve(collection, 0, upperLimit);
    }/* w ww  .  ja va  2  s  .  c  om*/

    private static void eratosthenesSieve(Collection<Integer> collection,
            int lowerLimit, int upperLimit) {
        boolean[] b = new boolean[upperLimit];
        b[0] = b[1] = true;
        for (int i = 2; i < upperLimit; i++) {
            if (!b[i]) {
                if (i > lowerLimit) {
                    collection.add(i);
                }
                for (int j = i * 2; j < upperLimit; j += i) {
                    b[j] = true;
                }
            }
        }
    }
}

Related Tutorials