Skip to content

Instantly share code, notes, and snippets.

@aoi0308
Created December 30, 2011 16:46
Show Gist options
  • Save aoi0308/1540570 to your computer and use it in GitHub Desktop.
Save aoi0308/1540570 to your computer and use it in GitHub Desktop.
1000個の素数をファイルにカンマ区切りで各行10個ずつ出力するプログラム
isPrimeNumber :: Int -> [Int] -> Bool
isPrimeNumber n = not . any ((== 0) . (mod n)) . filter (<= sr)
where
sr = floor $ sqrt $ fromInteger $ toInteger n
createPrimeList :: Int -> [Int]
createPrimeList 0 = []
createPrimeList len = _create 3 [2]
where
_create n ps
| len <= length ps = reverse ps
| isPrimeNumber n ps = _create (n + 2) (n : ps)
| otherwise = _create (n + 2) ps
mkString :: (Show a) => String -> [a] -> String
mkString _ [] = []
mkString d (x:[]) = (show x)
mkString d (x:xs) = (show x) ++ d ++ (mkString d xs)
make10 :: (Show a) => [a] -> [String]
make10 = _make . splitAt 10
where
_make (take10, []) = (mkString "," take10) : []
_make (take10, tail) = (mkString "," take10) : make10 tail
main :: IO()
-- main = putStrLn $ unlines $ make10 $ createPrimeList 1000
main = writeFile "prime.txt" $ unlines $ make10 $ createPrimeList 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment