Is Prime


#1

When i run this code it gives me an error message of Oops, try again. Your function fails on is_prime(1). It returns True when it should return False.

Shouldn't 1 be a prime number? Not sure what i can do to fix this. But would appreciate some help.


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


#2

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

So your error message tell you a god thing. Prime number must be greater then 1.
And if x < 2 and x != 0 is not a good way for finding a prime number as you can see a definition of prime number on the beginning.


#3

when i ran it without the x != 0 it errored out on zero.


#4

Your first line is one the right track, apart from the and x != 0 expression mentioned above. Any number less than 2 cannot be prime.

if number < 2:
    return False
if number > 2:
    # code
        return False

# number == 2 or survives the loop
return True

#5

Should not be inside the loop. See early post code skeleton.


#6

def is_prime(x):
    if x < 1 or x == 1:
       return False
    if x > 2: 
        if x % 2 == 0: 
            return False
    return True

This is my new code that works till i hit numbers like 9 that have a remainder. Not sure how to program in a check for remainders. Any suggestion?


#7

You still need a for loop:

for n in range(2, x):
    if x % n == 0:
        return False

#8

Ok thanks i got it now


#9

Keep it simple...

if x < 2:

#11