Hi! To answer your question, you could just tweak the sieve of Eratosthenes to get your answer.

Wikipedia lists the algorithm as:

```
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true.
for i = 2, 3, 4, ..., √n :
if A[i] is true:
for j = i^2, i^2+i, i^2+2i, ..., n:
A[j] := false
Now all i such that A[i] is true are prime.
```

You can modify it in this way:

```
Input: an integer n > 1
Let A be an array of Integer values, indexed by integers 2 to n,
initially all set to 0.
Let count be an integer set to 1 initially
for i = 2, 3, 4, ..., √n :
if A[i] = 0:
A[i] := count
count := count + 1
for j = i^2, i^2+i, i^2+2i, ..., n:
A[j] := -1
for i < n :
if A[i] = 0
A[i] := count
count := count + 1
Now all i such that A[i] > 0 are prime and A[i] denotes the index of this prime in the list of primes till n.
```

Hope this helps