Skip to content

Instantly share code, notes, and snippets.

@foresmac
Created March 1, 2016 17:59
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 foresmac/ce6d133e4cf5f2881e74 to your computer and use it in GitHub Desktop.
Save foresmac/ce6d133e4cf5f2881e74 to your computer and use it in GitHub Desktop.
# I found this on stack overflow but I forget where.
def is_prime(n):
""""pre-condition: n is a nonnegative integer
post-condition: return True if n is prime and False otherwise."""
if n < 2:
return False;
if n % 2 == 0:
return n == 2 # return False
k = 3
while k*k <= n:
if n % k == 0:
return False
k += 2
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment