Woodstock Blog

a tech blog for general algorithmic interview questions

[Question] Find 10001st Prime (Sieve of E)

Question

link

Find 10001st Prime Number

Analysis

Use Sieve of Eratosthenes, or 埃氏筛.

Code

private static final int INDEX = 10001;
private static final int LIMIT = 1000000;

private static int get10001stPrime() {
    boolean[] sieveArray = new boolean[LIMIT];
    int primeCount = 0;
    int currentNum = 2;
    while (primeCount < INDEX) {
        if (!sieveArray[currentNum]) {
            primeCount++;
            for (int i = currentNum; i < LIMIT; i += currentNum) {
                sieveArray[i] = true;
            }
        }
        currentNum++;
    }
    return currentNum - 1;
}