Skip to content

Instantly share code, notes, and snippets.

@smonteillet
Last active August 29, 2015 14:03
Show Gist options
  • Save smonteillet/56a6028876a794070f8c to your computer and use it in GitHub Desktop.
Save smonteillet/56a6028876a794070f8c to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes - Groovy
def allPrimes(max)
{
def range = 2..max
def primes = []
while(range.size() != 0)
{
def prime = range.first()
primes << prime
range = range.tail().grep { it % prime != 0 }
}
return primes
}
allPrimes(100).each{ println "$it" }
assert allPrimes(100) == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment