Skip to content

Instantly share code, notes, and snippets.

@psfblair
Created August 28, 2010 19:02
Show Gist options
  • Select an option

  • Save psfblair/555459 to your computer and use it in GitHub Desktop.

Select an option

Save psfblair/555459 to your computer and use it in GitHub Desktop.
(* 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