Efficient algorithm for generating all the k-almost prime numbers in a given range [a,b]:
func almost_primes(a, b, k) { a = max(2**k, a) var arr = [] func (m, lo, k) { var hi =idiv(b,m).iroot(k)if (k ==1) { lo = max(lo, idiv_ceil(a, m))each_prime(lo, hi, {|p| arr << m*p })returnnil }each_prime(lo, hi, {|p| var t = m*p var u = idiv_ceil(a, t) var v = idiv(b, t)nextif (u > v)__FUNC__(t,p, k-1) }) }(1,2, k)return arr.sort}for k in (1..5) { var (x=10, lo=1, hi=2) var arr = []loop { arr +=almost_primes(lo, hi, k)breakif (arr.len>= x) lo = hi+1 hi = 2*lo } say arr.first(x)}