6. is_prime


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

what exactly am i missing? it keeps saying fails on is_prime(0)


#2

Look. If you put in 0 as input, the for-loop will bound to these values for the first iteration (loop cycle)

for 0 in range(2,0):
 ...

You see that the range is not valid. Therefore the for-loop can't be entered for x <= 2. Even if the input x is 2 the range will be range(2,2) and in the end is not a valid range. For a loop cycle / iteration you need at least a range of range(2,3).

I would suggest you this:

def is_prime(x):
    if x <= 1:
        return False
    for i in range(2,x+1):
        if x%i ==0 and x!= i:
            return False
            break
        elif x%i ==0 and x== i:
            return True

As you can see in case of an input like 0 or 1 the foor loop will never be entered and the function will return false.


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

0xmoonstar's code works great but I found similarities in our code, so I thought I should share mine for a simpler method. You have to put the lines 4-5 and lines 9-10 in your code outside the for loop. Also, I don't think we need a break function. Be careful with the range.
Why did you declare another variable as n for 2 instead of just putting 2? There's nothing wrong with that, I'm asking because there must be a reason, I want to know. ^^

P.S: else function must be outside the for loop, so 2 or 3 won't be a trouble.


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

is_prime(2) won't work because range(2,1) isn't a valid range therefore the function would return NONE, the standard return value


#5

You have to put the last else statement outside the for loop.

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

#6

You mean:

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

The truth is that if you don't complete the if-statement with an else part everything is being seen as if it is in the else part. Therefore you could also skip else in this case:

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

This works as well. But is it clean coding?


#7

Yeah, that works too. I don't know about clean coding, but it's easier for sure, and gives the intended results.


#8

Ok I see:

x = 3
print range(2,x-1)

Returns an empty list for range(2,2). [] No error here...


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

Thanks for pointing out.


#10

Yep that was my problem as well had the else statement outside of the for loop. Yea yea! It works thanky thanky.


#11

:grinning:Thanks it helped