Skip to content

Instantly share code, notes, and snippets.

@banacorn
Created September 26, 2012 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banacorn/3789342 to your computer and use it in GitHub Desktop.
Save banacorn/3789342 to your computer and use it in GitHub Desktop.
fib 1000000
import Data.List
import Data.Bits
fib :: Int -> Integer
fib n = snd . foldl' fib' (1, 0) . dropWhile not $
[testBit n k | k <- let s = bitSize n in [s-1,s-2..0]]
where
fib' (f, g) p
| p = (f*(f+2*g), ss)
| otherwise = (ss, g*(2*f-g))
where ss = f*f+g*g
main = print $ fib 1000000
ghc fib.hs
time ./fib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment