Skip to content

Instantly share code, notes, and snippets.

@InfoSec812
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InfoSec812/b7f65111ef1503fe132a to your computer and use it in GitHub Desktop.
Save InfoSec812/b7f65111ef1503fe132a to your computer and use it in GitHub Desktop.
An implementation of the Sieve of Eratosthenes for calculating primes...
"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