# Is_prime

#1

I don't understand why there is the following error message :

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

this is my code

``````def is_prime(x):
if x < 2:
return False
else:
for n in range(2, x - 1):
if x / n != x and x / n != 1:
return False
else:
return True``````

Can someone help me?

#2

The test for divisibility is `modulo`

``x % n == 0``

when x is evenly divisible by n. This is the indicator of a non-prime.

There should not be an `else` inside the loop.

#3

Ok for the else outside the loop

But for the rest of the code, I don't understand why it can't work. May be I am too tired for understanding it. I'm going to the bed and I will try to understand tommorow morning...

#4

When is `x divided by n equal to x`? When `n` is `1`.

When is `x divided by n equal to 1`? When `n` is equal to `x`.

The above helps define what a Prime number is, but is not a test for primeness since it is not exhaustive (test for factors). That is why we need to cycle through numbers smaller than `x` starting with `2`.

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

The only values that will survive this loop will be the primes.

#5

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