Prime numbers (It ignores my if?)


I get the following error: Oops, try again. Your function fails on is_prime(5). It returns False when it should return True.

I expected it to say True but to me it looks like it ignores my if.
I know this is probably not the way it is meant te be but I still want to know what is going wrong here because to me it looks fine. I tried doing this a few other ways as well ( like using int() ) but I keep getting the same error.
Please tell me whats going wrong because its really annoying.

def is_prime(x):
    k = x / 2
    l = x // 2
    o = x / 3
    p = x // 3
    if x == 0 or x == 1:
        return False
    elif x == 2 or x == 3:
        return True
    elif k - l == 0:
        if o - p == 0:
            #Not Prime number
            return False
        return False
    elif k - l != 0 and o - p != 0:
        return True
        print 'you didn\'t type a number'


Are you sure that does what's required to determine if something is prime? Notice how, no matter how large x is (say, 1265978702083650944459424408709567540444823959358408332894144199), it will still do about the same number of operations like .. 10 or so. Is it really that easy to determine if a very large number is prime?
Hint: you're not expected to be able to handle a number as large as that one.


In short yes, I think that is how to calculate a prime number since I can't think of a number that fails my 'formula'.
Also even if it is the wrong way (or completely wrong) I still dont get why it is ignoring my if, since it seems it just goes to the first return without checking if the if statement is true or not.


/ and // do the same thing for integers, both do floor division

You seem to be trying to divide by only 2 and 3.

A prime is only divisible by 1 and itself. You'll have to try a whole lot more numbers than just 2 and 3.

For example, 25 is divisible by neither, but is divisible by 5 and is therefore not prime. Similarly, 35, 49, 55, 77, 121 are not primes but are not divisible by either of 2 or 3


Thank you!
That explains so much and I was already surprised there was so simple 'formula' for calculating a prime number :slight_smile: looks like there isn't and that is way more logical.
Again thank you now I can continue with trying different things :wink:.


Again thank you and thanks to you I now made this:

def is_prime(x):
    if x <= 1:
        return False
    elif x == 2 or x == 3:
        return True
    elif x > 3:
        n = x - 1
        while n >= 1:
            if x % n == 0:
                return False
            elif n == 2:
                #should be prime number
                return True
                n = n - 1

I tought you may find it intresting :slight_smile: