Skip to content

Instantly share code, notes, and snippets.

@honghaoz
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honghaoz/6904b5ac1579cc3d25aa to your computer and use it in GitHub Desktop.
Save honghaoz/6904b5ac1579cc3d25aa to your computer and use it in GitHub Desktop.
isPrime
def isPrime(n):
'''check if integer n is a prime'''
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# range starts with 3 and only needs to go up the squareroot of n
# for all odd numbers
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment