Implementation

In this lesson, we'll see the implementation of Sieve of Eratosthenes.

We'll cover the following

Implementation

The Boolean array iis_prime[]is\_prime[] of Boolean is initialized to truetrue and denotes that all numbers are assumed prime initially and marks them falsefalse as we proceed.

We start with i=2i=2 and only go up to ceil(N)ceil(\sqrt{N}) as explained in previous chapter.

Instead of comparing as i<=(N)i <= \sqrt(N), we are comparing as ii<=Ni*i <= N. It’s essentially the same comparison and we avoid dealing with the floating-point variable.

Get hands-on with 1200+ tech skills courses.