-
-
Save InfoSec812/b7f65111ef1503fe132a to your computer and use it in GitHub Desktop.
An implementation of the Sieve of Eratosthenes for calculating primes...
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
"Recursive function for iterating over a sequence of numbers" | |
by("Deven Phillips <dphillips@zanclus.com>") | |
shared [Integer+] sieve([Integer+] input) { | |
// Get the first element from the sequence | |
value prime = input.first; | |
// Test the initial prime and check factorization against every | |
// element left in the sequence | |
value rest = [ for (element in input) if (!prime.divides(element)) element ]; | |
// If the remaining sequence is empty, we're done. Otherwise recurse another level | |
if (nonempty rest) { | |
return [prime, *sieve(rest)]; | |
} else { | |
return [prime]; | |
} | |
} | |
"Sieve of Eratosthenes implementation as a recursive function" | |
by("Deven Phillips <dphillips@zanclus.com>") | |
shared void printPrimes(Integer upperLimit = 100) { | |
value initial = [ for (x in 2..upperLimit) x ]; | |
value result = sieve(initial); | |
print(",".join(result)); | |
} | |
shared void run() { | |
printPrimes(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment