Skip to content

Instantly share code, notes, and snippets.

@haiiro-shimeji
Last active December 10, 2015 15:18
Show Gist options
  • Save haiiro-shimeji/4453127 to your computer and use it in GitHub Desktop.
Save haiiro-shimeji/4453127 to your computer and use it in GitHub Desktop.
エラトステネスのふるい
primenumber :: Int -> [Int]
primenumber x
| 1 > x = error "argument 0 must be >0."
| 1 == x = []
primenumber x = sieve [2..x]
where
s = (sqrt . fromIntegral) x
sieve :: [Int] -> [Int]
sieve (x:[]) = x : []
sieve all@(x:xs)
| s < fromIntegral x = all
| otherwise = x : sieve [ x' | x' <- xs, 0 /= mod x' x ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment