Is_prime - Now it says 15 can't do it


#1

def is_prime(x):

if x < 2:
    return False

elif x == 2:
    return True

elif x == 9:
    return False

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

    else:
        return True

Now it says 15 can't do it


Is_prime
#2

elif x == 9:
return False

Why do you have this statment?


#3

The instructions for this exercise are pretty clear, and if we follow them without being too creative, we end with a basic brute force prime number tester.

  1. Primes are integers that are greater than 1.
  2. 2 is the only even Prime.
  3. Primes have no divisors other than themselves and 1.

We will not want our program to be testing for every number. That is for our loop to take care of. There are really only two conditionals given in the instructions.

if x < 2: return False

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

return True

#4

Wow! Thanks! Guess I overthought it too much.


#5

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