6\15 is_prime


#1

def is_prime(x):
    if x > 2:
        for n in range(2,x):
            if x % n != 0 :
                return True
                  
            else:
                return False
    elif x == 0 or x == 1:
        return False
    elif x == 2:
        return True
          
print is_prime(36)

So this is my code.
It works fine but! If x = 9 + 6 * N (N >= 0) it returns True instead of False....
And I tried to check my code with the numbers: 6, 9, 10, 15, 18 and 36. Besides 15 and 9 the code worked fine (returns False for the rest of the number which is good, that's what we want) but on 9 and 15 it returns True.
Can someone help me?


#2

I don't know who edited my code but, this code doesn't works as well. I already tried to do that code before I posted this post.


#3

i will tell you where you biggest problem is, then you can try again, if you need more help after that, please repost your code. Your biggest problem is that your loop:

for n in range(2,x):

will only execute once. What? once? yes, once. If the if statement is true in the first run of the loop, it will return true, and the loops ends. Is the if condition false, the else will execute, return False, the loops ends (once the return keyword is reached, a functions ends) To overcome this issue you could do the following:

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

make sure the indent is correct (crucial here), now if x is larger then 2, the loop will execute, the moment your if statement inside the loop is true, it will return True, if the loops execute without returning anything, False will be returned, hope this helps


#4

Got it!
The only thing missing in my code was approprite break :smile:
Thanks for the help!


#5

I edited it, but only to see your code with proper indention. Good you figured it out now


#6

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

After a lot of struggling this thing worked out. I used a counter to make sure that the number that the interpreter passes is 1 and check to see if it is prime or not. Just lemme know if something is not correct with this.
* List item


#7

After reading the above hints I still struggled. This is what I eventually came up with.

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

....elif x == 2:
........return True

....elif x <= 1:
........return False

After deciding if the number is greater than 2 you have to loop through a range of numbers starting from 2 that doesn't include the number itself because that would return a false positive because modulus would return a 0 indicating that the number is evenly divisible. Then one has to use an if statement to see if you can divide the number by by any numbers between 2 and one less than the number itself (luckily range only creates numbers from the starting number and one less than the ending number). If it can be so divided then the loop will terminate and return a False value indicating that the number is not a prime number (the loop terminating is important). If after looping through all numbers between 2 and the number itself no False value is returned, then the loop will return a True statement before ending normally (the return statement is indented the same as the for loop). So the key is to figure out if the number IS NOT a prime number before one actually discerns weather or not IT IS a prime number. Thinking in reverse is often the key to programming.

Finally make sure you add the elif statement that checks if the number is less than 1 so that negative numbers can be checked and the elif statement that checks if the number is equal to 2.


#8
def is_prime (x):
    t = [1,2,3,4,5,6,7,8,9]
    p = 1
    for e in t:
        if (x % e) == 0 and (x != e):
            p += 1
    if p > 2 or x == 1 or x < 0:
        return False
    else:
        return True

This is what I came up with that worked. I like that there are different ways to solve the same problem.


#9

This is the code I ended up with. I even had to explicitly add the "x%7==0" to be false and subsequently "x==7" to be true, because the site somehow got a wrong return at "is_prime(49)"

def is_prime(x):
    if x <= 1:
        return False
    elif x <= 3 or x == 7:
        return True
    elif x % 2 == 0 or x % 3 == 0 or x % 7 == 0:
        return False
    i = 5
    while i*i <= x:
        if x % i == 0 or x % i+2 == 0:
            return False
        i+=6
    return True

#10

def is_prime (x):
t = [1,2,3,4,5,6,7,8,9]
p = 1
for e in t:
if (x % e) == 0 and (x != e):
p += 1
if p > 2 or x == 1 or x < 0:
return False
else:
return True

@ rulemiester: this doesnt seem to be a correct solution; the code gives false positives if x is a number divisible by a factor greater than 9. (try 169 or 289, for instance.)

codecademy just doesnt happen to catch the error.


#11

@markrensen: this also doesnt seem to be a correct solution.

try 13*17 or 13*19, for instance.

i think you meant to increment i by 1 instead of by 6.


#12

Thanks for that, I see the error. I went back and wrapped my brain around it some more, and also gained some help from some co-workers and came up with this:

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

#13

Hi, may i ask there is a return True for the last line? Thanks!:grinning:


#14

What? Return True returns True when the number is prime, because the for loop haven't returned False


#15

Thank you. :grinning:


#16

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:
            print "not a prime number"
            return False
    else:
        return True

#17

You have a question @flipper?


#18

Was also struggling with this, looking at @flipper's code made my de-indent my last else: return True. Why does it work like this? I'd imagine the else: belongs to the if statement, and not to the for statement?

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
        # changed this:
            else:
                return True
        # to:
        else:
            return True

#19

No, it doesn't, if your else statement is on the same indent level as your if statement, either the if or the else statement will be true, the return statement gets executed, the function ends (if the function ends, the loop inside the function obviously stop as well), so your loop will stop in its first run


#20

Thanks for the help.
Did some searching on for...else, and learned something new: a for can have an else. For anyone interested, this made me realize: http://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops#