Skip to content

Instantly share code, notes, and snippets.

@joehakimrahme
Created July 4, 2012 16:00
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 joehakimrahme/3048050 to your computer and use it in GitHub Desktop.
Save joehakimrahme/3048050 to your computer and use it in GitHub Desktop.
isprime - 6k +/- 1
import math.sqrt as sqrt
def prime4(n):
if n<=1:
return False
if n % 2 == 0 and n != 2:
return False
if n % 3 == 0 and n != 3:
return False
# Limiting the tests to the 6k +/- 1 numbers
for i in (x for x in xrange(3, int(sqrt(n)), 2)
if x % 6 == 1 or x % 6 == 5):
if n % i == 0:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment