Groovy web console

subscribe to the feed Subscribe
to this
site
Primes below n (via #groovywebconsole)
tweet this snippet Tweet
this
script

Primes below n

Published 2 weeks ago by S.J.M with tags primes prime numbers prime
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
//  Find all prime numbers below n
//  A Groovy experiment by S.J.M

def primes(int n)
{
    a = []

for (i in 0..<n)
    {
        isPrime = true
        for (j in 2..<i)
        { if (i % j == 0)  isPrime = false; }
    
        if (isPrime) a << i
    }

    return a
}

nums = primes(1000)

for (j in 0..<a.size()) { println(nums[j]) }