Created
August 28, 2010 19:02
-
-
Save psfblair/555459 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| (* Recursive way - this gives the nth fibonacci number *) | |
| let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2) | |
| let fibonaccis = 0|> Seq.unfold (fun(x) -> Some(fib(x), x+1)) | |
| Seq.takeWhile (fun x -> x < 4000000) fibonaccis | |
| |> Seq.filter (fun x -> x % 2 = 0) | |
| |> Seq.fold (+) 0 | |
| (* Try putting the upper sequence limit into the sequence | |
| Something's wrong here: takes five minutes and the answer is wrong (1485607536) *) | |
| let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2) | |
| let fibonaccis = Seq.unfold( fun(x) -> if x >= 4000000 then None else Some(fib(x), x+1) ) 0 | |
| fibonaccis |> Seq.filter (fun x -> x % 2 = 0) | |
| |> Seq.fold (+) 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment