Last active
November 7, 2018 15:45
-
-
Save KSXGitHub/27a1ef89bfe636a8ef6ef56cead55aa1 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
primes = take 15 (sieve [2..]) | |
sieve (prime:rest) = prime : sieve [x | x <- rest, mod x prime /= 0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sieve = ([prime, ...rest]) => | |
prime ? [prime, ...sieve(rest.filter(x => x % prime !== 0))] : [] | |
const primes = sieve([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, /* ... */]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sieve (prime = None, *rest): | |
if not prime: return [] | |
return [prime, *sieve(*[x for x in rest if x % prime != 0])] | |
primes = sieve(*range(2, 30)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment