Fibonacci sequence: Difference between revisions
→{{header|F_Sharp|F#}}: comment on naive recursive sequence approach...
GordonBGood (talk | contribs) (→Better Recursive doesn't need Memoization: shorten...) |
GordonBGood (talk | contribs) (→{{header|F_Sharp|F#}}: comment on naive recursive sequence approach...) |
||
Line 2,428:
=={{header|F_Sharp|F#}}==
This is a fast [tail-recursive] approach using the F# big integer support
<lang fsharp>
let fibonacci n : bigint =
Line 2,439:
> fibonacci 100;;
val it : bigint = 354224848179261915075I</lang>
Lazy evaluated using sequence workflow:
<lang fsharp>let rec fib = seq { yield! [0;1];
for (a,b) in Seq.zip fib (Seq.skip 1 fib) -> a+b}</lang>
The above is extremely slow due to the nested recursions on sequences, which aren't very efficient at the best of times. The above takes seconds just to compute the 30th Fibonacci number!
Lazy evaluated using the sequence unfold anamorphism▼
▲Lazy
<lang fsharp>let fibonacci = Seq.unfold (fun (x, y) -> Some(x, (y, x + y))) (0I,1I)
fibonacci |> Seq.nth 10000
|