Created
December 30, 2011 16:46
-
-
Save aoi0308/1540570 to your computer and use it in GitHub Desktop.
1000個の素数をファイルにカンマ区切りで各行10個ずつ出力するプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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