Could the hints provided in is_prime could lead to an invalid algorithm


I think the hints provided in step two could lead to a wrong algorithm:

it says:

"For each number n from 2 to x - 1, test if x is evenly divisible by n."

This could translate to

for n in range(2, x-1):

The problem with this is that the algorithm will not recognize 2 or 3 because, if x=3, the for would be something like this:

for n in range(2, 3-1):

That would give us no iterations because it would mean: "from 2 to 2, do the following".

A similar problem applies to x=2:

for n in range(2, 2-1):

That would mean: "from 2 to 1 do the following:", which makes no sense at all.

Therefore, the algorithm will disregard the first two prime numbers!


take a look at this

def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        return True
    for n in range(2, x):
        if x % n == 0:
            return False
        return True