# 6. is_prime I see other people's answers being more complicated. Is my code sufficient for this lesson?

#1

### def is_prime(x):

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

#2

Also I've seen some answers consisting of for n in range(2,x) Is this the same as range(2, (x - 1))

#3

Both may be disregarded. Once we get to range(x/2 +1) there are no factors. It is just a brute force loop with little or no logical sense. That is to be ironed out later in our own study.

Eventually we hit upon the square root of x plus one as the range. That is the logical limit. And if we have a list of Primes up to that range, that is all we need really iterate, and nothing else.

#4

Mind that indentation is part of Python's way of distinguishing code blocks. It is critical.

``````def is_prime(x):
if x < 2: return False
if x > 2:
for n in range(2, int(x ** 0.5) + 1):
if x % n == 0: return False
return True``````

#5

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