6. is prime


#1

What is wrong with my code
def is_prime(x):
for n in range(2, x-1):
if x%n == 0:
return False
elif x < 2:
return False
else:
return True
is_prime(input())


#2

consider all numbers are prime
before returning True
check if x < 2
if not then check all numbers in range from 2 to x-1
here you need to remember that range function in python includes the first value but not the last one
after all return True


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

Above code worked for me, I think one key is to check x<2 condition outside the for loop. And like sercancelik said, python range() function is start-inclusive but end-exclusive, which means range(2, x) will include every integer from 2 up to x-1. Here we would like to test whether the x is evenly divisble by only 1 and x itself, which is the defination of prime numbers, so the end of range() should be set to x+1, which includes x itself.


#5

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

This one worked for me, but I made it really sketchy with elif... But as long as it worked... all good


#6

The above works well. Here is a slightly different way to do it that also works:

At first I thought Terarockstar had extra code in there by using x + 1, but that is just a clever way to return True instead of using the else in the for loop.


#7

@slythefantastic Using below code is giving me error:

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

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

I figured out that it is due to misplace of else in the code but I couldn't find why it won't work with else as I placed.


#8

This makes the most sense to me, code wise. On a self-deprecating side note, it pains me to know my most common short coming is understanding middle school level math...


#9

I agree Terrarockstar's way is a different solution and it works but I don't like having that check inside of the loop.

Since we know that x/n will = 1 when we reach n=x and our default behavior outside of the loop is to send "True" why have the code in the loop? We can handle that exception outside of the loop and save checking for that state every time the loop runs. We can take it a step further and not bother checking when n=x, as we know that will be true and can be matched by the else statement.

Your example is cleaner and would run faster especially with very large numbers. That said, both were great examples of solutions to the issue.


#10

Ha! I agree about middle school math... this learning code thing is a humbling experience.


#11

@codyroche I agree. That's why I posted the alternative. It seemed like an unnecessary way to do it. However, I also never thought of it that way, so it was good to see.


#12

Sorry for the late reply @ajaxsolver68476:

If you put three backticks (SHIFT + the key to the left of the 1 on most keyboards) before and after your code, then the formatting sticks like so:

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

Alternately, you can use the snipping tool, which is installed on most windows computers nowadays, which looks like this:

In any case, I'm not sure why that code isn't working for you. I've tried it again and it worked well. Maybe post your code using the backticks so we can see spacing/formatting.


#13

Bah, I got stuck on this problem and was racking my brain as to why it wasn't working. When I saw someone else's code looked the same as mind, I got even more frustrated.... until I noticed where my else statement was. Bah! shakes fist at pre-coffee brain


#14

could you help me understand whats wrong with this code?
def is_prime(x):

if x<2 :
    print"This number not prime"
    return False

elif x== 2 or x==3:
    print"This is  prime"
    return True

else:    
    for a in range(2,x):
        if x % a == 0:
            print"The number is not prime"
            return False

        else:
            print"The number is prime"
            return True

x=input("Enter a number: ")
is_prime(x)


#15

What's wrong with mine? Error is: Your function fails on is_prime(3). It returns None when it should return True.

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


#17

Fijense en el úlimo else, asi debe ir posicionado.


#19

check your indents
yours is exactly same as mine,
except for the 4,5th line

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


#20

@zeziba @slythefantastic
Please check this if it is wrong...it shows me an error...

Thanks in advance

--
Regards
Pradeep


#21

Already have a good post on this

Here


#22

@zeziba It was an awesome explanation thanks a lot. :smiley:

--
Regards
Pradeep


#23

I was wondering the exact thing! Thank you for pointing that out or I would've kept going crazy over it.