1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

今年の素数日を求める

Last updated at Posted at 2015-01-03

私も、『今年の素数日を計算してみる』に倣って「素数日」を調べるコードを書いてみました。

改変した点は "isPrimeDay" 関数と "primeDaysIn" 関数を独立させたことと、コマンドライン引数がない場合は今年の素数日を表示することです。

"isPrimeDay" 関数を独立させたので、特定の日が素数日かを調べたり、いろいろな範囲で素数日を探すことができるようになりました。

primeDaysIn.hs
import System.Environment
import Data.Time.Calendar
import Data.Time.LocalTime
import Data.Char


-- 素数か?
isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr f True ps
  where
    ps = (2 : 3 : concat [[6 * x - 1, 6 * x + 1] | x <- [1 ..]])
    f p r = (p * p > n) || (rem n p /= 0 && r)

-- 素数日か?
isPrimeDay :: Day -> Bool
isPrimeDay = isPrime . read . filter (/= '-') . show

-- 引数の年の素数日のリストを返す
primeDaysIn :: Integer -> [Day]
primeDaysIn y = filter isPrimeDay [fromGregorian y 1 1 .. fromGregorian y 12 31]


--
-- + コマンドライン引数で年を指定する。
-- + 引数がなければ今年の素数日を調べる。
--
main :: IO ()
main = do args <- getArgs
          let putUseage = putStrLn "Usage : primeDaysIn <year>"
          case args of
            y : _ -> if all isDigit y
                       then mapM_ print $ primeDaysIn $ read y
                       else putUseage
            _     -> do y <- getZonedTime
                        mapM_ print $ primeDaysIn $ read $ takeWhile (/= '-') $ show y
>runghc primeDaysIn.hs 2015
runghc primeDaysIn.hs 2015
2015-01-11
2015-01-31
2015-02-27
2015-03-03
2015-03-27
2015-04-11
2015-05-13
2015-08-21
2015-10-11
2015-10-31
2015-11-27
2015-12-21
2015-12-27
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?