【发布时间】:2022-01-11 06:11:48
【问题描述】:
我目前正在努力尝试在 haskell 中查找范围之间的素数。程序正确打印出素数范围。例如 countPrimesUntil 2 10 将打印出 [2, 3, 5, 7]。我正在寻找数字 4,因为这是 2 到 10 之间的素数。如何正确合并 countPrimes?
import Data.List
countPrimesUntil :: Integral a=> a -> a -> [a]
countPrimesUntil a b = takeWhile (<= b) $ dropWhile (< a) $ sieve [2..]
while sieve (n:ns) = n:sieve [m | m <- ns, m `mod` n /= 0]
countPrimes n = length([x | x <- [2..n], countPrimesUntil x])
【问题讨论】:
-
while应该是where
标签: haskell functional-programming primes