'''FROM SO'''
'''
Check for prime. Only need to iterate up to the square root of n because:
If n = a*b, its either a = b (each a sqrt of n) or a != b. If a != b,
either one of them must be less than the sqrt. Hence, only need to check up
to sqrt of n.
'''
import math
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
for i in range(3, int(math.sqrt(n)) + 1, 2): # Jump 2 number per iteration, only check odd ones
if n % i == 0:
return False
return True
'''
Note that this code does not properly handle 0, 1, and negative numbers.
We make this simpler by using all with a generator expression to replace the for-loop.
Doens't work with Python 3 though.
'''
import math
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
return all(n % i for i in range(3, int(math.sqrt(n)) + 1), 2)
'''
Jeff Knupp solution
'''
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):
if number % current == 0:
return False
return True
return False