# Is_prime and range(2,x-1)

#1

is_prime

I am wondering about the logic in the more common solution for this problem using for n in range(2,(x-1)):
Am I correct in thinking that the logic in this is to save from the other if statements of catching when x is ==2? And that it does not check 1 number away from the max of input through then on out. Such as if the input is 10 it will not check against 10%9 but it doesn't need to because we know that after 2 a number ,say y%(y-1), will never equal 0?

``````def is_prime_again(x):
if x>2:
for n in range(2,x):
print ""
print x
print n
print x%n
print range(2,x)
if x%n ==0:
print "not prime"
return False
break
return True
elif x==2:
print "2 is prime"
return True
else:
print "1 and less are not prime"
return False

print is_prime_again(10)``````

#2

don't be fooled by the forum (even i made that mistake ) this are the instructions:

``For each number n from 2 to x - 1, test if x is evenly divisible by n``

however, if you use range, the stop (x) is not included so `range(2,x)` is what you need to use, but many people get this wrong.

if you use `range(2,x)` you don't need a if statement to check if `x` equals two, you only need one for the lower numbers (smaller then 2)

#3

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