Skip to content

Almost prime

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
use Ghul.Pipes
smallest_factor(n: int) -> int =>
(2::n) |> find_or_throw(divisor => n % divisor == 0)
prime_factor_count(n: int) -> int =>
if n == 1 then 0 else 1 + prime_factor_count(n / smallest_factor(n)) fi
almost_primes(k: int) -> Pipe[int] is
let n mut = 2
do
if prime_factor_count(n) == k then
yield n
fi
n = n + 1
od
si
for k in 1::5 do
let first_ten = almost_primes(k) |> take(10) |> map(n => "{n,5}")
write_line("k = {k}:{first_ten |> join("")}")
od