Created
November 13, 2015 23:37
-
-
Save Isweet/adf05037da204d118205 to your computer and use it in GitHub Desktop.
A couple different Fibonacci's
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
import Data.Function.Memoize | |
-- SLOW | |
fib :: Int -> Integer | |
fib 0 = 0 | |
fib 1 = 1 | |
fib n = fib (n - 1) + fib (n - 2) | |
-- Memoized / FAST | |
fib' :: Int -> Integer | |
fib' = memoize h | |
where h 0 = 0 | |
h 1 = 1 | |
h n = fib' (n - 1) + fib' (n - 2) | |
fib'' :: Int -> Integer | |
fib'' = (map h [0..] !!) | |
where h 0 = 0 | |
h 1 = 1 | |
h n = fib'' (n - 1) + fib'' (n - 2) | |
main :: IO () | |
main = putStrLn . show $ fib' 30000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment