Is_prime


#1



I can't get past 4 with this code(the number not the instruction). I have no idea where to start on how to fix this. Thank you.


def is_prime(x):
    test = x % 2
    if x % 2 == 1 or x:
        while x != 1:
            return True
            break
        else:
            return False
    else:
        return False
print is_prime(4)


#2

First, think for a moment what defines a non-Prime: Divisibility. How are you factoring this rule into your code?


#5

I am trying to test if a number is prime with the modulo operator, which should return 0 when it is asked 4 % 2. I don't see where the code is messing up. Am I using the modulo operator wrong?


#6

Less wrong, more incompletely. We need to incorporate it dynamically to test against several divisors since we need to rule out all that could apply. This is a brute force program we're writing here so focus on how to test if 17 or 31 are prime and don't focus on 2 for now. The steps give you a range for your loop which is more than adequate for the brute force task.

if x % n == 0: return False

would be a line to look at and fashion into your solution.


#7

Ok so I added what you told me to add and now the code goes to 9 but thinks that 9 is a prime number. I know this is the first odd number it comes across that isn't a prime number. So I don't know how to tell the program to test if the odd numbers have any other factor than 1 and themselves. Thanks for the help.

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


#8

Should be at the top of the code so it doesn't have to be repeated.

if x < 2: return False
for n in range(2, x):
    # if line shown above
return True

Note there are no elif or else.


#9

I have updated my code and somehow 2 returns none, also when I change the range from (2, x) to (1, x) 9 returns false.
def is_prime(x):
if x <= 1:
return False
else:
for n in range(1, x):
if x % n == 0:
return False
elif x == 2:
return True
else:
return True
print is_prime(9)


#10

We would not include 1 in the range since everything is divisible by 1 whether Prime or not. The range starts at 2.

Consider that when we return from an if statement, we do not need an else.

if x < 2: return False
for ...

return True

As we can see, if x is less than 2 we exit the function. If not, then we can procede directly to the loop with range(2, x). When x == 2 the loop does not run. 2 drops down to the last line, return True. This means we do not need to test if x == 2.


#11

So I reformatted the code again and got rid of some things I don't think I needed but 4 fails and I'm pretty sure it thinks every number 2 and up is a prime number.
def is_prime(x):
if x < 2:
return False
for n in range(2, x):
if x % n == 0:
return False
else:
return True
return True
print is_prime(9)


#12

Remove these lines. There should be no else: branch in the if statement`.


#13

Thank you so much for helping. I realized after that the issue was a simple indentation with the for loop. Again much gratitude for all the help you gave me.


#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.