Gettin’ my Groovy On

For one reason or another, I’ve finally decided that I needed to get familiar with Groovy.

As always, I start with the Sieve of Eratosthenes. This finds all the primes between 2 and 100 — you can substitute 100 on the first line for any arbitrary integer and it will run accordingly

nums = (2..100)
primes =[]
comps=[]

nums.each { item->
    if (! comps.contains(item)) {
        primes.add(item)

        multi=item*2
        while (multi<=nums.max()) {
            comps.add(multi)
            multi+=item
        }
    }
}

println primes

Comments are closed.