4 ms·
For example like this (it provides exact result Edit—no matter how big n is). CReal is some magic. import Data.Number.CReal fib :: Integer -> Integer fi
by lelf 13y ago
For example like this (it provides exact result Edit—no matter how big n is). CReal is some magic.
import Data.Number.CReal
fib :: Integer -> Integer
fib n = round $ (φ^^n - (-φ)^^(-n)) / sqrt 5
where
φ = (1 + sqrt 5) / 2 :: CReal
-- While we at it
fib' n = fibs !! n
where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
- carterschonwald 13y agoYeah, the constructive reals lib is really cool, and that's definitely an example where it really shines.
- Peaker 13y agoAnother one: fibs = map fst (iterate (\(cur,next)->(next,cur+next)) (0,1))