通过奇数序列求解的素数序列:
isprime n = not $ elem 0 $ map (\x -> mod n x) $ takeWhile (\x -> x^2 <= n) (2:[3,5..]) primelist = [x | x <- 2:[3,5..], isprime x]通过递归定义的素数序列:
isprime n = not $ elem 0 $ map (\x -> mod n x) $ takeWhile(\x -> x^2 <= n) primelist primelist = 2:[x | x <- [3,5..], isprime x]
没有评论:
发表评论