Skip to content

Instantly share code, notes, and snippets.

@akihiro4chawon
Created November 30, 2014 07:27
Show Gist options
  • Save akihiro4chawon/f38603e39f4651f52ab5 to your computer and use it in GitHub Desktop.
Save akihiro4chawon/f38603e39f4651f52ab5 to your computer and use it in GitHub Desktop.
久しぶりにリハビリ
module Main where
import Data.Array
-- mod m としたフィボナッチ数列 (繰り返しとなる時点で打ち切り)
fibMod :: Int -> [Int]
fibMod m = 0 : fibMod' 1 1
where
fibMod' 0 1 = []
fibMod' x y = x : fibMod' y (mod (x + y) m)
-- フィボナッチ数列の第n番目の値を返す表を作成する
createTable :: Int -> Array Int Int
createTable m = listArray (0, length fibModList) fibModList
where
fibModList = fibMod m
-- フィボナッチ数列の第n番目の値
fibTermNthMod :: Int -> Int -> Int
fibTermNthMod m n = table ! mod n size
where
table = createTable m
size = snd $ bounds table
main :: IO ()
main = getLine >> interact interaction
where
interaction = unlines . map perLine . lines
perLine = show . fibTermNthMod 1000 . read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment