Step 1: Gathering Prime Number Candidates
We'll start by creating an array of integers up to n+1
. Since prime numbers must be natural numbers greater than 1, we can discard 0
and 1
.
Here's a visual representation of what we're doing:

Here's how we initialize our candidates in both JavaScript and Python:
1const n = 4;
2const numsLessThanN = Array.from(
3 { length: n + 1 }, function(val, idx) {
4 return idx;
5 }
6).slice(2);
This provides us with all potential prime number candidates smaller than n
.