Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

A faster isprime check

Checking even numbers before and then only looping over only odd numbers makes this twice as fast.

def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 for i in range(3,int(math.sqrt(n)),2):
 if n % i == 0:
 return False
 return True

An even faster isprime check

All primes (past 2 and 3) are of the forms 6n+1 and 6n-1.

def isprime(n):
 # The same as above
 
primes = []
for i in range(6,MAX,6):
 if isprime(i+1):
 primes.append(i+1)
 elif isprime(i-1):
 primes.append(i-1)

An extremly fast prime checker

If you need to make several test in a row, the Sieve of Erastothenes is the best method

MAX = 1000 # depends on your needs
def eratosthenes(n):
 # Taken from Rosetta Code
 multiples = set()
 for i in range(2, n+1):
 if i not in multiples:
 print(i)
 multiples.update(range(i*i, n+1, i))
primes = eratosthenes(MAX)
def is_prime(n,primes):
 return n in primes

If you are really really desperate about performance look here for faster-than-you-can-imagine implementations of the sieve of Eratostenes look here for faster-than-you-can-imagine implementations of the sieve of Eratostenes.

A faster isprime check

Checking even numbers before and then only looping over only odd numbers makes this twice as fast.

def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 for i in range(3,int(math.sqrt(n)),2):
 if n % i == 0:
 return False
 return True

An even faster isprime check

All primes (past 2 and 3) are of the forms 6n+1 and 6n-1.

def isprime(n):
 # The same as above
 
primes = []
for i in range(6,MAX,6):
 if isprime(i+1):
 primes.append(i+1)
 elif isprime(i-1):
 primes.append(i-1)

An extremly fast prime checker

If you need to make several test in a row, the Sieve of Erastothenes is the best method

MAX = 1000 # depends on your needs
def eratosthenes(n):
 # Taken from Rosetta Code
 multiples = set()
 for i in range(2, n+1):
 if i not in multiples:
 print(i)
 multiples.update(range(i*i, n+1, i))
primes = eratosthenes(MAX)
def is_prime(n,primes):
 return n in primes

If you are really really desperate about performance look here for faster-than-you-can-imagine implementations of the sieve of Eratostenes.

A faster isprime check

Checking even numbers before and then only looping over only odd numbers makes this twice as fast.

def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 for i in range(3,int(math.sqrt(n)),2):
 if n % i == 0:
 return False
 return True

An even faster isprime check

All primes (past 2 and 3) are of the forms 6n+1 and 6n-1.

def isprime(n):
 # The same as above
 
primes = []
for i in range(6,MAX,6):
 if isprime(i+1):
 primes.append(i+1)
 elif isprime(i-1):
 primes.append(i-1)

An extremly fast prime checker

If you need to make several test in a row, the Sieve of Erastothenes is the best method

MAX = 1000 # depends on your needs
def eratosthenes(n):
 # Taken from Rosetta Code
 multiples = set()
 for i in range(2, n+1):
 if i not in multiples:
 print(i)
 multiples.update(range(i*i, n+1, i))
primes = eratosthenes(MAX)
def is_prime(n,primes):
 return n in primes

If you are really really desperate about performance look here for faster-than-you-can-imagine implementations of the sieve of Eratostenes.

corrected post
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137
def primeisprime(n):
 """
 If this returns False, then you can be 100% sure that n is not prime.
 If this returns True, than n **may** be prime.
 >>> prime(25)
 True # 25 of courseThe issame notas primeabove
 """
primes = []
for i returnin range(n % 6 == 5) or (n % ,MAX,6 == 1)
def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 if n % 3 == 0 and n != 3:
 return False
 if not primeisprime(ni+1):
 return False
 for i in range(3,int(mathprimes.sqrtappend(n)),2i+1):
 if n %elif isprime(i == 0-1):
 return False
 return True
 primes.append(i-1)
def prime(n):
 """
 If this returns False, then you can be 100% sure that n is not prime.
 If this returns True, than n **may** be prime.
 >>> prime(25)
 True # 25 of course is not prime
 """
 return (n % 6 == 5) or (n % 6 == 1)
def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 if n % 3 == 0 and n != 3:
 return False
 if not prime(n):
 return False
 for i in range(3,int(math.sqrt(n)),2):
 if n % i == 0:
 return False
 return True
 
def isprime(n):
 # The same as above
 
primes = []
for i in range(6,MAX,6):
 if isprime(i+1):
 primes.append(i+1)
 elif isprime(i-1):
 primes.append(i-1)
final link
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137
def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 for i in range(3,int(math.sqrt(n)),2):
 if n % i == 0:
 return False
 return True
def prime(n):
 """
 If this returns False, then you can be 100% sure that n is not prime.
 If this returns True, than n **may** be prime.
 >>> prime(25)
 True # 25 of course is not prime
 """
 return (n % 6 == 5) or (n % 6 == 1)
def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 if n % 3 == 0 and n != 3:
 return False
 if not prime(n):
 return False
 for i in range(3,int(math.sqrt(n)),2):
 if n % i == 0:
 return False
 return True
 

If you are really really desperate about performance look here for faster-than-you-can-imagine implementations of the sieve of Eratostenes .

def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 for i in range(3,math.sqrt(n),2):
 if n % i == 0:
 return False
 return True
def prime(n):
 """
 If this returns False, then you can be 100% sure that n is not prime.
 If this returns True, than n **may** be prime.
 >>> prime(25)
 True # 25 of course is not prime
 """
 return (n % 6 == 5) or (n % 6 == 1)
def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 if n % 3 == 0 and n != 3:
 return False
 if not prime(n):
 return False
 for i in range(3,math.sqrt(n),2):
 if n % i == 0:
 return False
 return True
 
def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 for i in range(3,int(math.sqrt(n)),2):
 if n % i == 0:
 return False
 return True
def prime(n):
 """
 If this returns False, then you can be 100% sure that n is not prime.
 If this returns True, than n **may** be prime.
 >>> prime(25)
 True # 25 of course is not prime
 """
 return (n % 6 == 5) or (n % 6 == 1)
def isprime(n):
 if n < 2:
 return False
 if n % 2 == 0 and n != 2:
 return False
 if n % 3 == 0 and n != 3:
 return False
 if not prime(n):
 return False
 for i in range(3,int(math.sqrt(n)),2):
 if n % i == 0:
 return False
 return True
 

If you are really really desperate about performance look here for faster-than-you-can-imagine implementations of the sieve of Eratostenes .

more precise explanation
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137
Loading
error fixed
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137
Loading
removed broken code
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137
Loading
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /