Implementation of eratosthenes sieve in Groovy.
Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Reference: http://docs.groovy-lang.org/latest/html/documentation/#_range_operator
In mathematics, the sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit.
For more info take a look at reference.
Used groovy features (examples in OperationsTest
):
- range operator
..
expect: 5..Math.sqrt(40) == [5, 6]
- overloaded
-
on listsexpect: [1] - 1 == []
- step
expect: (1..10).step(5) == [1, 6]
and the algorithm is a simple combination of features mentioned above:
static def get(limit) {
def sieve = 2..limit
(2..Math.sqrt(limit)).each { k -> sieve -= ((2*k)..(sieve.last()+1)).step(k) }
return sieve
}