Created
March 18, 2011 04:50
-
-
Save dmitric/875632 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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