Semiprime
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
is_semiprime(n: int) -> bool => (
let rest mut = n
let count mut = 0
let divisor mut = 2
while divisor * divisor <= rest /\ count < 3 do
while rest % divisor == 0 do
rest = rest / divisor
count = count + 1
od
divisor = divisor + 1
od
if rest > 1 then
count = count + 1
fi
count == 2
)
let up_to_100 = (2::100) |> filter(is_semiprime) |> join(", ")
write_line("semiprimes up to 100: {up_to_100}")
for n in [1679, 1234, 5, 9, 2093] do
write_line(
"{n} is {if is_semiprime(n) then "" else "not " fi}semiprime")
od