Last active
August 29, 2015 14:03
-
-
Save smonteillet/56a6028876a794070f8c to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes - Groovy
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 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