You can save half the calculations by skipping the even numbers:
def isPrime(n):
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
i = 3
while i * i <= n:
if n % i == 0:
return False
i += 2
return True
Sorry, it got messed up, and apparently won't let me edit it:
from math import sqrt
def isPrimeNumber(number):
if number <= 1 or number % 2 == 0:
return False
if number == 2:
return True
for divisor in xrange(3, int(sqrt(number))+1, 2):
if number % divisor == 0:
return False
return True`
There is another variant without using sqrt:
Looks nicer if you use
count
from theitertools
module:You can save half the calculations by skipping the even numbers:
yeah, thx guys!
This is the fastest python prime detecting function I've found:
def isPrimeNumber(number): if number <= 1: return False
Sorry, it got messed up, and apparently won't let me edit it: