Skip to content

Instantly share code, notes, and snippets.

@ngsw-taro
Created August 26, 2012 01:26
Show Gist options
  • Save ngsw-taro/3472962 to your computer and use it in GitHub Desktop.
Save ngsw-taro/3472962 to your computer and use it in GitHub Desktop.
はすけるで素数判定
module Prime(
prime
) where
prime :: Int -> Bool
prime x | x < 0 = error "the argument should be larger than zero."
| x == 1 = False
| x == 2 = True
| mod x 2 == 0 = False
| otherwise = all (\y -> mod x y /= 0 || x == y) (take (x - 3) [3, 5..])
import Test.HUnit
import Prime
tests = test [prime 1 ~?= False,
prime 2 ~?= True,
prime 3 ~?= True,
prime 4 ~?= False,
prime 5 ~?= True,
prime 6 ~?= False,
prime 7 ~?= True,
prime 8 ~?= False,
prime 9 ~?= False,
prime 10 ~?= False,
prime 11 ~?= True,
prime 12 ~?= False,
prime 13 ~?= True,
prime 14 ~?= False,
prime 15 ~?= False,
prime 16 ~?= False,
prime 17 ~?= True,
prime 18 ~?= False,
prime 19 ~?= True,
prime 20 ~?= False]
main = do
runTestTT tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment