6. is prime: gettting error that says 2 is a prime


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#


why should I return true for a value of 2?

Oops, try again. Your function fails on is_prime(2). It returns False when it should return True.


Code works on python 3.5.1 with any number I feed it. I should expect it to work here.


def is_prime(x):
    
    if x < 2 or x % 2 == 0:
        return False
    else:
        for n in range(3,x-1):
            print n
            if x % n == 0:
                return False
    return True


#2

Becaue 2 is a prime.

Your function returns False for 2 because
x % 2 == 0
is True.

But an integer is only non-prime if there is a smaller integer that divides it evenly. Otherwise there wouldn't by any primes any number dividies itself without remainder.


#3

sorry, read it wrong


#4

No need to be sorry, I hope I was able to help :relaxed:


#5

We can solve the issue in the above line:

if x < 2 or x % 2 == 0 and x !== 2:

The above line should be removed or commented.

We don't need the else since the line above contains a return statement. Flow will fall through if not returned.

The range can be all the way up to x though it needn't be any more than x / 2, or even better, (x) ** 0.5 + 1 (one more than the square root of x).


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.