|
|
@ -6,7 +6,7 @@ app "prime"
|
|
|
|
main : Task.Task {} I32
|
|
|
|
main : Task.Task {} I32
|
|
|
|
main =
|
|
|
|
main =
|
|
|
|
numStrings =
|
|
|
|
numStrings =
|
|
|
|
List.range {start: At 0u64, end: At 1_000_000u64}
|
|
|
|
List.range { start: At 0u64, end: At 1_000_000u64 }
|
|
|
|
|> List.map \e -> isPrimePre e
|
|
|
|
|> List.map \e -> isPrimePre e
|
|
|
|
|> List.walk [] primeWalk
|
|
|
|
|> List.walk [] primeWalk
|
|
|
|
|> List.map Num.toStr
|
|
|
|
|> List.map Num.toStr
|
|
|
@ -21,11 +21,13 @@ primeWalk = \l, n ->
|
|
|
|
when n is
|
|
|
|
when n is
|
|
|
|
Yes n2 ->
|
|
|
|
Yes n2 ->
|
|
|
|
List.append l n2
|
|
|
|
List.append l n2
|
|
|
|
|
|
|
|
|
|
|
|
Maybe n2 ->
|
|
|
|
Maybe n2 ->
|
|
|
|
if isPrimePost l n2 then
|
|
|
|
if isPrimePost l n2 then
|
|
|
|
List.append l n2
|
|
|
|
List.append l n2
|
|
|
|
else
|
|
|
|
else
|
|
|
|
l
|
|
|
|
l
|
|
|
|
|
|
|
|
|
|
|
|
No _ -> l
|
|
|
|
No _ -> l
|
|
|
|
|
|
|
|
|
|
|
|
isPrimePre : U64 -> PrimeResult
|
|
|
|
isPrimePre : U64 -> PrimeResult
|
|
|
@ -34,20 +36,20 @@ isPrimePre = \n ->
|
|
|
|
No n
|
|
|
|
No n
|
|
|
|
else if n < 4 then
|
|
|
|
else if n < 4 then
|
|
|
|
Yes n
|
|
|
|
Yes n
|
|
|
|
else if ( firstPrimes |> List.any \p -> n > p && n % p == 0 ) then
|
|
|
|
else if (firstPrimes |> List.any \p -> n > p && n % p == 0) then
|
|
|
|
No n
|
|
|
|
No n
|
|
|
|
else
|
|
|
|
else
|
|
|
|
Maybe n
|
|
|
|
Maybe n
|
|
|
|
|
|
|
|
|
|
|
|
isPrimePost : List U64, U64 -> Bool
|
|
|
|
isPrimePost : List U64, U64 -> Bool
|
|
|
|
isPrimePost = \l, n ->
|
|
|
|
isPrimePost = \l, n ->
|
|
|
|
nFloat = (Num.toF64 n)
|
|
|
|
nFloat = Num.toF64 n
|
|
|
|
valLimit = Num.ceiling (Num.sqrt nFloat)
|
|
|
|
valLimit = Num.ceiling (Num.sqrt nFloat)
|
|
|
|
# approximate the number of primes < sqrt(n) to limit the range we have to check with a 25% error tolerance
|
|
|
|
# approximate the number of primes < sqrt(n) to limit the range we have to check with a 25% error tolerance
|
|
|
|
valLimitF = Num.toF64 valLimit
|
|
|
|
valLimitF = Num.toF64 valLimit
|
|
|
|
limit = Num.ceiling (valLimitF * 1.25 / (Num.log valLimitF))
|
|
|
|
limit = Num.ceiling (valLimitF * 1.25 / (Num.log valLimitF))
|
|
|
|
l
|
|
|
|
l
|
|
|
|
|> List.sublist {start: 0, len: limit}
|
|
|
|
|> List.sublist { start: 0, len: limit }
|
|
|
|
|> List.dropIf \p -> p > valLimit
|
|
|
|
|> List.dropIf \p -> p > valLimit
|
|
|
|
|> List.any \p -> n % p == 0
|
|
|
|
|> List.any \p -> n % p == 0
|
|
|
|
|> Bool.not
|
|
|
|
|> Bool.not
|
|
|
|