get Primes Between - Java java.lang

Java examples for java.lang:int prime

Description

get Primes Between

Demo Code


//package com.java2s;

import java.util.Collection;

public class Main {
    public static void getPrimesBetween(Collection<Integer> collection,
            int lowerLimit, int upperLimit) {
        eratosthenesSieve(collection, lowerLimit, upperLimit);
    }/*w  ww . j  a v a2 s  . c o m*/

    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