Here is a pretty clear explanation of this concept.

**Goal:** Find all primes which are less than 30.

**Step 1:** List 2-N

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
**Step 2:** Take 2 as prime, and remove all multiples of 2 up to N.

__2__ 3

~~4~~ 5

~~6~~ 7

~~8~~ 9

~~10~~ 11

~~12~~ 13

~~14~~ 15

~~16~~ 17

~~18~~ 19

~~20~~ 21

~~22~~ 23

~~24~~ 25

~~26~~ 27

~~28~~ 29

~~30~~
**Step 3:** Take 3 as prime, and remove all multiples of 3 up to N.

__2__ __3__ ~~4~~ 5

~~6~~ 7

~~8~~ ~~9~~ ~~10~~ 11

~~12~~ 13

~~14~~ ~~15~~ ~~16~~ 17

~~18~~ 19

~~20~~ ~~21~~~~22~~ 23

~~24~~ 25

~~26~~ ~~27~~ ~~28~~ 29

~~30~~
**Step 4:** Take 5 as prime, and remove all multiples of 5 up to N.

__2__ __3__ ~~4~~ __5__ ~~6~~ 7

~~8~~ ~~9~~ ~~10~~ 11

~~12~~ 13

~~14~~ ~~15~~ ~~16~~ 17

~~18~~ 19

~~20~~ ~~21~~~~22~~ 23

~~24~~ ~~25~~ ~~26~~ ~~27~~ ~~28~~ 29

~~30~~
Since 5 is the largest integer that is smaller than the square root of N, we can stop here. All number that have not been removed are primes. This leaves us with the following set...

{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}

This algorithm is considered good for small values of N (small in computer terms, so up to 10,000,000 should be ok). You can further enhance the algorithm by skipping all multiples between the current prime and its square. So for instance, in this example when we took 5 as prime, we could skip straight to 25, since we can be sure all other multiples between 5 and 25 (10, 15, 20) would have already been removed. This may not seem like much now, but it can save a bunch of time when your range spans millions.