Yet another Sieve of Eratosthenes in Java I wanted a reusable class for generating a list of primes till some arbitrary (but reasonable) limit. So, I wrote a class Primes that implements the Sieve of Eratosthenes and another method that makes use of this sieve. The implementation is done on the basis of clarity and swiftly obtaining results.

I admit that using a BitSet would have improved memory usage and using the Sieve of Atkin would have given me a (slightly) better runtime, but I avoided extra trouble and went for it.

Primes.java

/**
 * This is a utility class that contains methods related to prime numbers
 * that are used in many problems in Project Euler.
 *
 * @author Subhomoy Haldar
 * @version 1.0
 */
public class Primes {

    /**
     * Returns a boolean array that is essentially a sieve. In order to check
     * if a number is prime, check if the boolean at that index is true. For
     * example,
     * &lt;pre&gt;&lt;code&gt;
     *  // ...
     *  boolean[] sieve = Prime.siftTill(someLimit);
     *  int n = (yourNumber);
     *  if (sieve[n])
     *      // ... do operations with the prime number
     *  // ...
     * &lt;/code&gt;&lt;/pre&gt;
     * &lt;p&gt;
     * &lt;h2&gt;Time complexity:&lt;/h2&gt;
     * O(n log log n)
     * &lt;h2&gt;Space complexity:&lt;/h2&gt;
     * O(n)
     *
     * @param n The number till which the sieve must be generated.
     * @return The sieve in the form of a boolean array.
     */
    public static boolean[] siftTill(int n) {
        // create an array of size (n + 1), to include the index n as well
        boolean[] sieve = new boolean[++n];
        // the outer loop will be limited to ~ sqrt(n) iterations
        int limit = (int) Math.sqrt(n);
        // initially set all to true except 0 and 1
        for (int i = 2; i &lt; n; i++) sieve[i] = true;
        // start from 2 and remove all multiples of primes
        for (int i = 2; i &lt; limit; i++) {
            if (sieve[i]) {
                for (int j = i * i; j &lt; n; j += i) {
                    sieve[j] = false;
                }
            }
        }
        return sieve;
    }

    /**
     * Returns an array of primes upto the given inclusive limit.
     * &lt;p&gt;
     * &lt;h2&gt;Time complexity:&lt;/h2&gt;
     * O(n)
     * &lt;h2&gt;Space complexity:&lt;/h2&gt;
     * O(n)
     *
     * @param limit The inclusive upper limit.
     * @return An array of primes upto the given inclusive limit.
     */
    public static long[] getPrimesTill(int limit) {
        boolean[] isPrime = siftTill(limit);
        // 1st pass: count the number of primes
        int count = 0, index = 0;
        for (int i = 2; i &lt;= limit; i++) {
            if (isPrime[i])
                count++;
        }
        long[] primes = new long[count];
        // 2nd pass: store the primes
        for (int i = 2; i &lt;= limit; i++) {
            if (isPrime[i])
                primes[index++] = i;
        }
        return primes;
    }
}


I welcome comments on all aspects of the code.
Documentation
The class shouldn't mention who the intended client is, just what it does.
Most of your comments inside methods are noise. Document why, not what.
Error Handling
You don't handle bad inputs (e.g. negative values). It doesn't make sense for siftTill() to return an ArrayIndexOutOfBoundsException in that case. Explicitly throw a relevant exception, like IllegalArgumentException.
Naming
You use 'till' as an abbreviation for 'until'. Don't! A &quot;till&quot; is a real word which means something different, and it may be confusing, especially since 'until' only has one 'l'.
Best Practices
Don't reassign method parameters.
Declare variables as close as possible to their first use.
Don't use increment/decrement operations inside array references. Junior programmers will not be able to tell the order of operations.
It doesn't make sense to have a getPrimesTill() take an int as the maximum value but return a long[].
You're better off keying loops off of the array's size explicitly, rather than the variable you used to set its length. That way, if either changes your array traversal won't break.
The Primes class should be final and have a private constructor to indicate it should not be instantiated. Alternately, make an instantiable class that can be optionally be reused and runs the sieve for numbers it hasn't yet computed. That lets you keep computed information, such as the number of primes, in a member variable rather than having to walk your sieve array to find it.
Putting it all together might look something like:
import java.util.Arrays;

/**
 * This is a utility class that contains methods related to prime numbers.
 *
 * @author Subhomoy Haldar
 * @version 1.0
 */
public final class Primes {

    /**
     * Returns a boolean array that is essentially a sieve. In order to check
     * if a number is prime, check if the boolean at that index is true. For
     * example,
     *
     * &lt;pre&gt;
     * &lt;code&gt;
     *  // ...
     *  boolean[] sieve = Prime.siftTill(someLimit);
     *  int n = (yourNumber);
     *  if (sieve[n])
     *      // ... do operations with the prime number
     *  // ...
     * &lt;/code&gt;
     * &lt;/pre&gt;
     * &lt;p&gt;
     * &lt;h2&gt;Time complexity:&lt;/h2&gt;
     * O(n log log n)
     * &lt;h2&gt;Space complexity:&lt;/h2&gt; O(n)
     *
     * @param n The number until which the sieve must be generated.
     * @return The sieve in the form of a boolean array.
     */
    public static boolean[] siftUntil(final int n) {

        final boolean[] sieve = new boolean[n + 1];
        Arrays.fill(sieve, true);
        sieve[0] = false;
        sieve[1] = false;

        removePrimeMultiplesFrom(sieve);

        return sieve;
    }

    private static void removePrimeMultiplesFrom(final boolean[] sieve) {
        final int limit = (int) Math.sqrt(sieve.length);
        for (int i = 2; i &lt; limit; i++) {
            if (!sieve[i]) {
                continue;
            }

            for (int j = i * i; j &lt; sieve.length; j += i) {
                sieve[j] = false;
            }
        }
    }

    /**
     * Returns an array of primes up to the given inclusive limit.
     * &lt;p&gt;
     * &lt;h2&gt;Time complexity:&lt;/h2&gt;
     * O(n)
     * &lt;h2&gt;Space complexity:&lt;/h2&gt; O(n)
     *
     * @param limit The inclusive upper limit.
     * @return An array of primes up to the given inclusive limit.
     */
    public static int[] getPrimesUntil(final int limit) {

        final boolean[] isPrime = siftUntil(limit);

        final int[] primes = new int[countPrimes(isPrime)];
        for (int i = 2, primeIndex = 0; i &lt;= limit; i++) {
            if (isPrime[i]) {
                primes[primeIndex] = i;
                primeIndex++;
            }
        }
        return primes;
    }

    private static int countPrimes(final boolean[] isPrime) {
        int count = 0;
        for (int i = 2; i &lt; isPrime.length; i++) {
            if (isPrime[i]) {
                count++;
            }
        }
        return count;
    }

}