Skip to content

Instantly share code, notes, and snippets.

@wiccy46
Last active April 26, 2020 09:16
Show Gist options
  • Save wiccy46/36f1ec3c7e2767a56b628f9b25cf235e to your computer and use it in GitHub Desktop.
Save wiccy46/36f1ec3c7e2767a56b628f9b25cf235e to your computer and use it in GitHub Desktop.
[find_primes] Several find primes method, Yield, Use sieve of eratosthenes to find primes. #python #math
# Method 1 # sieve of eratosthenes
def primes(n):
i, p, ps, m = 0, 3, [2], n // 2
sieve = [True] * m
while p <= n:
if sieve[i]:
ps.append(p)
for j in range(int((p*p-3)/2), m, p):
sieve[j] = False
i, p = i+1, p+2
return ps
# Method 2 # Yield
def check_prime(n):
for divisor in range(2, int(n ** 0.5) + 1):
if n % divisor == 0:
return False
return True
def Primes(max):
number = 1
while number < max:
number += 1
if check_prime(number):
yield number
list(Primes(1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment