Skip to content

Instantly share code, notes, and snippets.

@dmitric
Created March 18, 2011 04:50
Show Gist options
  • Save dmitric/875632 to your computer and use it in GitHub Desktop.
Save dmitric/875632 to your computer and use it in GitHub Desktop.
def naive_is_prime(n):
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in xrange(3,n):
if n % i == 0:
return False
return True
import math
def is_prime(n):
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
max_possible = long(math.floor(math.sqrt(n))) #the sqrt is the max possible unique factor
for i in xrange(3,max_possible+1,2):
if n % i == 0:
return False
return True
import re
#A clever, but inefficient way using regexes
def is_prime_regex(n):
if re.match(r'^1?$|^(11+?)\1+$','1'*n) == None:
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment