Why is Fibonacci misimplemented so often?

https://news.ycombinator.com/item?id=274186
by globalrev • 18 years ago
3 4 18 years ago

Why is it that so many times, even among good programmers, the Fibonacci function is misimplemented?

Here it is from the OCaml tutorial:

<<#let rec fib n = # if n < 2 then 1 else fib(n-1) + fib(n-2);; val fib : int -> int = <fun>

  #fib 10;;
  - : int = 89
>>

Finonacci 0 is 0, not 1. Thus: Fibonacci 10 is 55.

Corrected:

# let rec fibo n = # if n < 2 then if n ==1 then 1 else 0 else fibo(n-1) + fibo(n-2);; val fibo : int -> int = <fun> # fibo 12;; - : int = 144 # fibo 10;; - : int = 55 #

(And yes it is a superslow exponential implementation but that is not the point here.)

Related Stories

Loading related stories...

Source preview

news.ycombinator.com