#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Almost_prime#Sidef
#

func is_k_almost_prime(n, k) {
    for (var (p, f) = (2, 0); (f < k) && (p*p <= n); ++p) {
        (n /= p; ++f) while (n %% p);
    }
    f + (n > 1 ? 1 : 0) == k
}

for k in (1..5) {
    var x = 10
    say gather {
        for i in (1 .. Inf) {
            if (is_k_almost_prime(i, k)) {
                take(i); (--x).is_zero && break;
            }
        }
    }
}
