stuff
This commit is contained in:
parent
f6620624df
commit
4f3056c84b
@ -1,5 +1,7 @@
|
|||||||
# Codewars solutions in python
|
# Codewars solutions in python
|
||||||
|
|
||||||
|
![](https://www.codewars.com/users/thib8956/badges/small?theme=light)
|
||||||
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
|
|
||||||
```console
|
```console
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
def sieve_np(limit):
|
def sieve_np(limit):
|
||||||
if limit < 2:
|
if limit < 2:
|
||||||
return []
|
return []
|
||||||
@ -17,6 +18,7 @@ def sieve_np(limit):
|
|||||||
primes = np.where(is_prime)[0]
|
primes = np.where(is_prime)[0]
|
||||||
return primes.tolist()
|
return primes.tolist()
|
||||||
|
|
||||||
|
|
||||||
def sieve(max_n):
|
def sieve(max_n):
|
||||||
s = [True] * (max_n + 1)
|
s = [True] * (max_n + 1)
|
||||||
p = 2
|
p = 2
|
||||||
@ -25,34 +27,31 @@ def sieve(max_n):
|
|||||||
for i in range(p**2, max_n + 1, p):
|
for i in range(p**2, max_n + 1, p):
|
||||||
s[i] = False
|
s[i] = False
|
||||||
p += 1
|
p += 1
|
||||||
|
|
||||||
primes = [p for p in range(2, max_n + 1) if s[p]]
|
primes = [p for p in range(2, max_n + 1) if s[p]]
|
||||||
return primes
|
return primes
|
||||||
|
|
||||||
|
|
||||||
def sieve_of_eratosthenes(limit):
|
def sieve(max_n):
|
||||||
if limit < 2:
|
s = [True] * (max_n + 1)
|
||||||
return []
|
|
||||||
|
|
||||||
# Create a boolean array "prime[0..limit]" and initialize all entries as True
|
|
||||||
prime = [True] * (limit + 1)
|
|
||||||
prime[0] = prime[1] = False # 0 and 1 are not prime numbers
|
|
||||||
|
|
||||||
p = 2
|
p = 2
|
||||||
while p**2 <= limit:
|
while p**2 <= max_n:
|
||||||
if prime[p]:
|
if s[p]:
|
||||||
# Update all multiples of p starting from p^2
|
for i in range(p**2, max_n + 1, p):
|
||||||
prime[p**2 : limit + 1 : p] = [False] * len(prime[p**2 : limit + 1 : p])
|
s[i] = False
|
||||||
p += 1
|
p += 1
|
||||||
|
|
||||||
# Create a list of prime numbers
|
primes = [p for p in range(2, max_n + 1) if s[p]]
|
||||||
primes = [p for p in range(2, limit + 1) if prime[p]]
|
|
||||||
return primes
|
return primes
|
||||||
|
|
||||||
|
|
||||||
|
_primes = sieve(25_000_000)
|
||||||
|
|
||||||
|
|
||||||
class Primes:
|
class Primes:
|
||||||
primes = sieve(25_000_000)
|
global _primes
|
||||||
|
primes = _primes
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def stream():
|
def stream():
|
||||||
yield from Primes.primes
|
yield from Primes.primes
|
||||||
|
Loading…
Reference in New Issue
Block a user