# [Erlang] Fibonacci Numbers

Page 1 of 1

## 0 Replies - 1173 Views - Last Post: 17 December 2010 - 01:04 PM

### #1 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12209
• Posts: 45,288
• Joined: 27-December 08

# [Erlang] Fibonacci Numbers

Posted 17 December 2010 - 01:04 PM

Description: Compile the fibonacci.erl file, and invoke fibonacci:fib(n). On the Erlang Shell: c(fibonacci). fibonacci:fib(10). %returns 55Returns the nth Fibonacci Number
```-module(fibonacci).
-export([fib/1]).

%the 0th Fibonacci number is 0
fib(N) when N == 0 -> 0;

%the first two Fibonacci numbers are 1
fib(N) when (N < 3) and (N > 0) -> 1;

%Otherwise, calculate the nth fibonacci number
%by the definition Fib(n) = Fib(n-1) + fib(n-2)
fib(N) when N > 0 -> fib(N-1) + fib(N-2).

```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }