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

#1 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • 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