app "prime2" packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.1/Icc3xJoIixF3hCcfXrDwLCu4wQHtNdPyoJkEbkgIElA.tar.br" } imports [pf.Stdout, pf.Task] provides [main] to pf main : Task.Task {} I32 main = numStrings = primesIn 50_000_000 |> List.map Num.toStr |> Str.joinWith "\n" Stdout.line "$(numStrings)" primesIn : U64 -> List U64 primesIn = \n -> numbers = List.concat [2] (List.range { start: At 3u64, end: At n, step: 2 }) primeSieve numbers primeSieve : List U64 -> List U64 primeSieve = \l -> when l is [] -> [] [a] -> [a] [head, .., tail] if (Num.powInt head 2) > tail -> l [head, .. as tail] -> filteredList = List.dropIf tail (\e -> Num.isMultipleOf e head) List.concat [head] (primeSieve filteredList) countPrimesIn : U64 -> Nat countPrimesIn = \limit -> List.range { start: At 2, end: At limit } |> List.countIf \n -> if n < 4 then Bool.true else if Num.isEven n then Bool.false else res = List.range { start: At 3, end: At (Num.ceiling (Num.sqrt (Num.toF64 n))), step: 2 } |> List.any \a -> Num.isMultipleOf n a |> Bool.not res expect ( countPrimesIn 100000 |> \expected -> dbg expected expected ) == ( List.len (primesIn 100000) |> \actual -> dbg actual actual )