n & 1 returns the last bit of number, which is 1 for odd numbers (True like, e.g. 7 = 0b111) and 0 for even (e.g. 4 = 0b100)

``````def isprime(x):
for i in range(2,x):
if x%i == 0:
return False
elif: x%i != 0 and i == x-1:
return True
``````

A simple timing example:

``````import time

def print_timing(func):
"""
a decorator function to time another function
time.clock() works on Windows systems only
you can use time.time() instead, but it gets updated less
frequently and can give 0.0 results on faster functions
"""
def inner(*arg):
"""*arg are the arguments of function func"""
t1 = time.clock()
res = func(*arg)
t2 = time.clock()
# time is in milliseconds
print('%s took %0.3f ms' % (func.__name__, (t2-t1)*1000.0))
return res
return inner

@print_timing
def isprime(x):
for i in range(2, x):
if x%i == 0:
return False
elif x%i != 0 and i == x-1:
return True

@print_timing
def isprime5(n):
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
sqr = int(n**0.5)
f = 5
while f <= sqr:
if n%f == 0: return False
if n%(f+2) == 0: return False
# loop every 6th integer
f += 6
return True

isprime(13999979)  # isprime took 404.684 ms

isprime5(13999979) # isprime5 took 0.022 ms

isprime5(135999979) # isprime5 took 0.610 ms

isprime(135999979)  # MemoryError
``````
``````def isprime(x):
if x%2 != 0:
for i in range(3,x,2):
if x%i == 0:
return False
elif x%i != 0 and i == x-2:
return True
#A much faster way of processing the code#
``````
``````def isprime(x):
if x%2 != 0:
for i in range(3,x,2):
if x%i == 0:
return False
elif x%i != 0 and i == x-2:
return True
elif x == 0 or x == 1 or x == 2:
return False
else:
return False
``````

correction of above, more efficient

``````for num in range(3,40):  #put the range of numbers
isp = 0
for j in range(2,num//2+1):
if(num%j==0):
isp = 1
break
if(isp!=1):
print(num)``````
``````def is_prime(k):
for u in range(2, (k // 2) + 1):
if k % u == 0:
return False
return True``````