Jump to content

Fibonacci sequence: Difference between revisions

→‎{{header|F_Sharp|F#}}: comment on naive recursive sequence approach...
(→‎{{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 evaluatedevaluation using the sequence unfold anamorphism is much much better as to efficiency:
<lang fsharp>let fibonacci = Seq.unfold (fun (x, y) -> Some(x, (y, x + y))) (0I,1I)
fibonacci |> Seq.nth 10000
474

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.