#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Sidef
#

func sieve_iter(limit) {
    var is_prime = [false, false, ([true] * limit-1)...]
    gather {
        is_prime.each_kv { |number, prime|
            if (prime) {
                take(number)
                for n in range(number**2, limit, number) { is_prime[n] = false }
            }
        }
    }
}

func sieve(Number n) { sieve(@(2..n)) }
func sieve(Array a   { .first > .last.sqrt }) { a }
func sieve(Array a)  { [a[0], sieve(a.grep { !(_ %% a[0]) })...] }  #%

var s1 = sieve_iter(100).join(",");
var s2 = sieve(100).join(",");

say s1;
assert_eq(s1, s2);
