Fibonacci Numbers Back to the Win32 Shootout Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]
All Source For Fibonacci Numbers
fibo.awka
```# \$Id: fibo.gawk,v 1.4 2001/05/20 06:13:00 doug Exp \$
# http://www.bagley.org/~doug/shootout/

function fib(n) {
if (n < 2) return(1);
return(fib(n-2) + fib(n-1));
}

BEGIN {
n = (ARGV[1] < 1) ? 1 : ARGV[1];
printf("%d\n", fib(n));
exit;
}
```
fibo.bcc
```/* -*- mode: c -*-
* \$Id: fibo.gcc,v 1.2 2000/12/24 19:10:50 doug Exp \$
* http://www.bagley.org/~doug/shootout/
*/

#include <stdio.h>
#include <stdlib.h>

unsigned long
fib(unsigned long n) {
if (n < 2)
return(1);
else
return(fib(n-2) + fib(n-1));
}

int
main(int argc, char *argv[]) {
int N = ((argc == 2) ? atoi(argv[1]) : 1);
printf("%ld\n", fib(N));
return(0);
}
```
fibo.bigforth
```\ \$Id: fibo.bigforth,v 1.1 2001/06/19 16:20:46 doug Exp \$
\ fibonacci numbers
\ http://www.bagley.org/~doug/shootout/

\ read NUM from last command line argument
0. argc @ 1- arg >number 2drop drop constant NUM

\ compute fibonacci numbers
: fib  recursive
dup 2 <
if
drop 1
else
dup
2 - fib
swap
1 - fib
+
then ;

NUM fib 1 u.r cr

bye \ th-th-that's all folks!
```
fibo.cim
```% \$Id: fibo.cim,v 1.2 2002/05/16 15:28:00 dada Exp \$
external class UNIX;
begin

integer procedure fib(n); integer n;
begin
if n < 2 then fib := 1
else fib := fib(n-2) + fib(n-1);
end;

integer i;
text t;
t :- Blanks(80);
t := Arg(1);
comment OutInt(Argc, 4);
comment OutText(Arg(1));
i := t.getInt;
if i < 1 then i := 1;
OutInt(fib(i),0);
OutImage;
end
```
fibo.csharp
```// \$Id: fibo.csharp,v 1.0 2002/02/14 10:49:00 dada Exp \$

using System;

class App {

public static int fib(int n) {
return (n < 2) ? 1 : fib(n-2) + fib(n-1);
}
public static int Main(String[] args) {
int N;
int f;
N = System.Convert.ToInt32(args[0]);
if(N < 1) N = 1;
f = fib(N);
Console.WriteLine(f.ToString()+"\n");
return(0);
}
}
```
fibo.cygperl
```#!/usr/local/bin/perl
# \$Id: fibo.perl,v 1.4 2001/02/20 02:12:08 doug Exp \$
# http://www.bagley.org/~doug/shootout/

use strict;
use integer;

# from Leif Stensson
sub fib {
return \$_[0] < 2 ? 1 : fib(\$_[0]-2) + fib(\$_[0]-1);
}

my \$N = (\$ARGV[0] < 1) ? 1 : \$ARGV[0];
my \$fib = fib(\$N);
print "\$fib\n";
```
fibo.delphi
```program fibo;

function fib(N : cardinal) : cardinal;
begin
if N < 2 then fib := 1
else fib := fib(N-2) + fib(N-1);
End;

var NUM, code : integer;
begin
NUM :=1;
if ParamCount=1 then
Val(ParamStr(1),NUM,code);

WriteLn(fib(NUM));
end.

```
fibo.elastic
```// \$Id: fibo.elastic,v 1.0 2002/04/19 11:54:00 dada Exp \$
package fibo;

import basic;
import sys;
import array;

private function fib( n )
{
if (n < 2) return(1);
return(fib(n-2) + fib(n-1));
}

private n = 1;
if(array.length(sys.args) > 0) {
n = basic.int(sys.args[0]);
} else {
n = 1;
}
basic.print(fib(n));

```
fibo.erlang
```%%% -*- mode: erlang -*-
%%% \$Id: fibo.erlang,v 1.2 2000/12/24 19:10:50 doug Exp \$
%%% http://www.bagley.org/~doug/shootout/

-module(fibo).
-export([main/1]).

main() -> main(['1']).
main([Arg]) ->
Num = list_to_integer(atom_to_list(Arg)),
io:fwrite("~w\n", [fib(Num)]),
halt(0).

fib(N) when N < 2 -> 1;
fib(N) -> fib(N-2) + fib(N-1).
```
fibo.fpascal
```program fibo;
uses SysUtils;

function fib(N : integer) : longint;
begin
if N < 2 then fib := 1
else fib := fib(N-2) + fib(N-1);
End;

var
NUM : integer;
f : longint;

begin
if ParamCount = 0 then
NUM := 1
else
NUM := StrToInt(ParamStr(1));

if NUM < 1 then NUM := 1;
f := fib(NUM);
WriteLn( IntToStr(f) );
end.
```
fibo.gawk
```# \$Id: fibo.gawk,v 1.4 2001/05/20 06:13:00 doug Exp \$
# http://www.bagley.org/~doug/shootout/

function fib(n) {
if (n < 2) return(1);
return(fib(n-2) + fib(n-1));
}

BEGIN {
n = (ARGV[1] < 1) ? 1 : ARGV[1];
printf("%d\n", fib(n));
exit;
}
```
fibo.gcc
```/* -*- mode: c -*-
* \$Id: fibo.gcc,v 1.2 2000/12/24 19:10:50 doug Exp \$
* http://www.bagley.org/~doug/shootout/
*/

#include <stdio.h>
#include <stdlib.h>

unsigned long
fib(unsigned long n) {
if (n < 2)
return(1);
else
return(fib(n-2) + fib(n-1));
}

int
main(int argc, char *argv[]) {
int N = ((argc == 2) ? atoi(argv[1]) : 1);
printf("%ld\n", fib(N));
return(0);
}
```
fibo.gforth
```\ \$Id: fibo.gforth,v 1.2 2001/05/25 16:45:07 doug Exp \$
\ fibonacci numbers
\ http://www.bagley.org/~doug/shootout/

\ read NUM from last command line argument
0. argc @ 1- arg >number 2drop drop constant NUM

\ compute fibonacci numbers
: fib  recursive
dup 2 <
if
drop 1
else
dup
2 - fib
swap
1 - fib
+
then ;

NUM fib 1 u.r cr

bye \ th-th-that's all folks!
```
fibo.ghc
```-- \$Id: fibo.ghc,v 1.4 2001/06/14 23:55:06 doug Exp \$
-- http://www.bagley.org/~doug/shootout/

import System(getArgs, getProgName, exitWith, ExitCode(..))

main = do
arg <- getArgs
case arg of
[number] -> putStrLn (show (fib (read number)))
_        -> do
progname <- getProgName
putStrLn ("Usage: " ++ progname ++ " number")
exitWith (ExitFailure 1)

fib :: Int -> Int
fib 0 = 1
fib 1 = 1
fib n = fib (n-2) + fib (n-1)
```
fibo.gnat
```-- \$Id: fibo.gnat,v 1.0 2003/06/11 12:02:00 dada Exp \$
-- Ada 95 code by C.C.

procedure Fibo is
function Fib (N : Natural) return Integer is
L        : Integer := N - 1;
pragma Suppress (Range_Check, On => L);
begin
if L <= 0 then
return 1;
else
return Fib (N - 2) + Fib (L);
end if;
end Fib;
N        : Natural := 0;
begin
begin
exception
when Constraint_Error => null;
end;
Ada.Integer_Text_IO.Put (Item => Fib (N), Width => 0);
end Fibo;

```
fibo.guile
```#!/usr/local/bin/guile \
-e main -s
!#
;;; \$Id: fibo.guile,v 1.5 2001/06/29 23:12:37 doug Exp \$
;;; http://www.bagley.org/~doug/shootout/

(define (main args)
(let ((n (or (and (= (length args) 2) (string->;number (cadr args))) 1)))
(display (fib n))
(newline)))

(define (fib n)
(cond ((<; n 2) 1)
(else (+ (fib (- n 2)) (fib (- n 1))))))

```
fibo.ici
```// \$Id: fibo.ici,v 1.0 2003/01/03 11:14:00 dada Exp \$
//
// contributed by Tim Long

static
fib(n)
{
return n < 2 ? 1 : fib(n - 2) + fib(n - 1);
}

printf("%d\n", fib(argv[1] ? int(argv[1]) : 1));
```
fibo.icon
```# -*- mode: icon -*-
# \$Id: fibo.icon,v 1.2 2000/12/24 19:10:50 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
n := argv[1] | 1;
write(fib(n));
end

procedure fib(n)
if (n < 2) then { return(1) }
return(fib(n-2) + fib(n-1))
end
```
fibo.java
```// \$Id: fibo.java,v 1.2 2000/12/24 19:10:50 doug Exp \$
// http://www.bagley.org/~doug/shootout/

public class fibo {
public static void main(String args[]) {
int N = Integer.parseInt(args[0]);
System.out.println(fib(N));
}
public static int fib(int n) {
if (n < 2) return(1);
return( fib(n-2) + fib(n-1) );
}
}
```
fibo.jscript
```// -*- mode: java -*-
// \$Id: fibo.njs,v 1.1 2001/07/08 20:20:06 doug Exp \$
// http://www.bagley.org/~doug/shootout/
// by David Hedbor <david@hedbor.org>
// modified by Aldo Calpini <dada@perl.it> for Win32

function fib(n) {
if (n < 2) return 1;
return fib(n-2) + fib(n-1);
}

var n;
ARGS = WScript.Arguments;
if(ARGS.length > 0) {
n = parseInt(ARGS.Item(0), "10");
if(n < 1) n = 1;
} else {
n = 1;
}
WScript.Echo(fib(n));
```
fibo.lcc
```/* -*- mode: c -*-
* \$Id: fibo.gcc,v 1.2 2000/12/24 19:10:50 doug Exp \$
* http://www.bagley.org/~doug/shootout/
*/

#include <stdio.h>
#include <stdlib.h>

unsigned long
fib(unsigned long n) {
if (n < 2)
return(1);
else
return(fib(n-2) + fib(n-1));
}

int
main(int argc, char *argv[]) {
int N = ((argc == 2) ? atoi(argv[1]) : 1);
printf("%ld\n", fib(N));
return(0);
}
```
fibo.lua
```-- \$Id: fibo.lua,v 1.2 2000/12/24 19:10:50 doug Exp \$
-- http://www.bagley.org/~doug/shootout/

function fib(n)
if (n < 2) then return(1) end
return( fib(n-2) + fib(n-1) )
end

N = tonumber((arg and arg[1])) or 1
write(fib(N), "\n")
```
fibo.lua5
```-- \$Id: fibo.lua,v 1.2 2000/12/24 19:10:50 doug Exp \$
-- http://www.bagley.org/~doug/shootout/
-- contributed by Roberto Ierusalimschy

function fib(n)
if (n < 2) then return 1 end
return fib(n-2) + fib(n-1)
end

N = tonumber((arg and arg[1])) or 1
print(fib(N))

```
fibo.mawk
```# \$Id: fibo.mawk,v 1.2 2001/05/20 06:13:00 doug Exp \$
# http://www.bagley.org/~doug/shootout/

function fib(n) {
if (n < 2) return(1);
return(fib(n-2) + fib(n-1));
}

BEGIN {
n = (ARGV[1] < 1) ? 1 : ARGV[1];
printf("%d\n", fib(n));
exit;
}
```
fibo.mercury
```%% \$Id: fibo.mercury,v 1.3 2001/05/13 01:22:35 doug Exp \$
%% http://www.bagley.org/~doug/shootout/

:- module mytest.

:- interface.

:- import_module io.

:- pred main(io__state, io__state).
:- mode main(di, uo) is det.

:- implementation.

:- func fib(int) = int.
:- mode fib(in) = out is det.

:- import_module string, list, int.

:- pred mytest(int, io__state, io__state).
:- mode mytest(in, di, uo) is det.

fib(N) = R :-
( if N < 2 then
R = 1
else
R = fib(N-2) + fib(N-1)
).

mytest(Num) -->
io__write_int(fib(Num)), io__write_string("\n").

main -->
io__command_line_arguments(Args),
( if { Args = [] } then
mytest(1)
else if { Args = [Arg|_] } then
( if { string__to_int(Arg, N), N > 0 } then
mytest(N)
else
mytest(1)
)
).

```
fibo.mingw32
```/* -*- mode: c -*-
* \$Id: fibo.gcc,v 1.2 2000/12/24 19:10:50 doug Exp \$
* http://www.bagley.org/~doug/shootout/
*/

#include <stdio.h>
#include <stdlib.h>

unsigned long
fib(unsigned long n) {
if (n < 2)
return(1);
else
return(fib(n-2) + fib(n-1));
}

int
main(int argc, char *argv[]) {
int N = ((argc == 2) ? atoi(argv[1]) : 1);
printf("%ld\n", fib(N));
return(0);
}
```
fibo.modula2
```(* The Great Win32 Language Shootout http://dada.perl.it/shootout/

contributed by Isaac Gouy (Modula2 novice)

To build: xc =m fibo
To run:     fibo 32
*)

MODULE Fibo;

(* Prefer qualified procedures *)
IMPORT STextIO, SWholeIO, LanguageShootout;

PROCEDURE Fibo (n: CARDINAL) : CARDINAL;
BEGIN
IF n<2 THEN
RETURN 1;
ELSE
RETURN Fibo(n-2) + Fibo(n-1);
END;
END Fibo;

BEGIN
SWholeIO.WriteCard( Fibo( LanguageShootout.N() ),1);
STextIO.WriteLn;
END Fibo.
```
fibo.modula3
```
MODULE Main;
IMPORT Fmt, Scan, Params, Wr, Stdio;

PROCEDURE fib(n: INTEGER): INTEGER =
BEGIN
IF n < 2 THEN
RETURN 1;
ELSE
RETURN fib(n-2) + fib(n-1);
END
END fib;

VAR N: INTEGER;
BEGIN
IF Params.Count > 0 THEN
N := Scan.Int(Params.Get(1));
ELSE
N := 1;
END;
Wr.PutText (Stdio.stdout, Fmt.Int(fib(N)));
Wr.Close (Stdio.stdout);
END Main.
```
fibo.nice
```/* The Great Win32 Language Shootout http://dada.perl.it/shootout/
contributed by Isaac Gouy (Nice novice)

To compile:
nicec --sourcepath=.. -d=. -a fibo.jar fibo

To run:
java -jar fibo.jar 32
*/

import ackermann; // reuse toSingleInt

void main(String[] args){
println( fib( toSingleInt(args) ) );
}

int fib(int n){
if (n < 2) return 1; else return fib(n-2) + fib(n-1);
}```
fibo.ocaml
```(*
* \$Id: fibo.ocaml,v 1.3 2001/01/08 03:01:50 doug Exp \$
* http://www.bagley.org/~doug/shootout/
*)

let rec fib n =
if n < 2 then 1
else fib (n - 2) + fib (n - 1)

let _ =
let n =
try int_of_string Sys.argv.(1)
with Invalid_argument _ -> 1 in
Printf.printf "%d\n" (fib n)
```
fibo.ocamlb
```(*
* \$Id: fibo.ocaml,v 1.3 2001/01/08 03:01:50 doug Exp \$
* http://www.bagley.org/~doug/shootout/
*)

let rec fib n =
if n < 2 then 1
else fib (n - 2) + fib (n - 1)

let _ =
let n =
try int_of_string Sys.argv.(1)
with Invalid_argument _ -> 1 in
Printf.printf "%d\n" (fib n)
```
fibo.oz
```%%% \$Id: fibo.oz,v 1.0 2002/03/11 15:11:00 dada Exp \$
functor
import System Application
define
fun {Fib N}
case N
of 0 then 1
[] 1 then 1
else {Fib N-2} + {Fib N-1} end
end
in
local A in
[A] = {Application.getArgs plain}
{System.printInfo {Fib {String.toInt A}}}
end
{Application.exit 0}
end
```
fibo.parrot
```
set I0, P0[1]
bsr FIBO
print I0
print "\n"
end

FIBO:
lt I0, 2, FIBO_ONE
set I1, I0
set I2, I1
sub I2, 2
set I0, I2
save I1
save I2
save I3
bsr FIBO
restore I3
restore I2
restore I1
set I1, I0
inc I2
set I0, I2
save I1
save I2
save I3
bsr FIBO
restore I3
restore I2
restore I1
branch FIBO_END
FIBO_ONE:    set I0, 1
FIBO_END:
ret
```
fibo.perl
```#!/usr/local/bin/perl
# \$Id: fibo.perl,v 1.4 2001/02/20 02:12:08 doug Exp \$
# http://www.bagley.org/~doug/shootout/

use strict;
use integer;

# from Leif Stensson
sub fib {
return \$_[0] < 2 ? 1 : fib(\$_[0]-2) + fib(\$_[0]-1);
}

my \$N = (\$ARGV[0] < 1) ? 1 : \$ARGV[0];
my \$fib = fib(\$N);
print "\$fib\n";
```
fibo.php
```<?php
/*
\$Id: fibo.php,v 1.1 2001/05/06 03:07:33 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
function fibo(\$n){
return((\$n < 2) ? 1 : fibo(\$n - 2) + fibo(\$n - 1));
}
\$n = (\$argc == 2) ? \$argv[1] : 1;
\$r = fibo(\$n);
print "\$r\n";
?>
```
fibo.pike
```#!/usr/local/bin/pike// -*- mode: pike -*-
// \$Id: fibo.pike,v 1.2 2000/12/24 19:10:50 doug Exp \$
// http://www.bagley.org/~doug/shootout/

int
fib(int n) {
if (n < 2) return(1);
return( fib(n-2) + fib(n-1) );
}

void
main(int argc, array(string) argv) {
int n = max((int)argv[-1], 1);
write("%d\n", fib(n));
}

```
fibo.pliant
```module "/pliant/language/context.pli"

function fibo n -> r
arg Int n r
if n < 2
return 1
else
return (fibo n-2) + (fibo n-1)

gvar Str s_n := cast ((pliant_script_args translate Address 1) map CStr) Str
if (s_n parse (gvar Int n))
console (fibo n) eol
else
console "usage: fibo.pli <number>" eol
```
fibo.poplisp
```;;; \$Id: fibo.poplisp,v 1.0 2002/04/22 16:51:00 dada Exp \$

(defun fib (n)
(if (<; n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))

(let
((n (parse-integer (or (car pop11::poparglist) "1"))))
(prin1 (fib n))
)
```
fibo.python
```#!/usr/local/bin/python
# \$Id: fibo.python,v 1.2 2000/12/24 19:10:50 doug Exp \$
# http://www.bagley.org/~doug/shootout/

import sys

def fib(n):
if (n < 2):
return(1)
return( fib(n-2) + fib(n-1) )

def main():
N = int(sys.argv[1])
#sys.setrecursionlimit(3000)
print fib(N)

main()
```
fibo.rebol
```REBOL [
Title:   "Fibonacci"
Author:  "Aldo Calpini"
Date:    03-Jul-2001
File:    %fibo.r
]

Fib: func [N] [
return either N < 2  [ 1 ] [ (Fib N - 2) + (Fib N - 1) ]
]

NUM: to-integer to-string system/script/args
NUM: either NUM < 1 [ 1 ] [ NUM ]
R: Fib NUM
write %output.rebol rejoin [ R ]
```
fibo.rexx
```
parse arg n
If n < 1 Then Do
n = 1
End

R = fib(N)

say R
exit

fib:
PROCEDURE
PARSE ARG N
IF N<2 THEN RETURN 1
RETURN fib(N-2) + fib(N-1)
```
fibo.ruby
```#!/usr/local/bin/ruby
# -*- mode: ruby -*-
# \$Id: fibo.ruby,v 1.2 2000/12/24 19:10:50 doug Exp \$
# http://www.bagley.org/~doug/shootout/

def fib(n)
if n < 2 then
1
else
fib(n-2) + fib(n-1)
end
end

N = Integer(ARGV.shift || 1)
puts fib(N)

```
fibo.se
```-- -*- mode: eiffel -*-
-- \$Id: fibo.se,v 1.2 2000/12/24 19:10:50 doug Exp \$
-- http://www.bagley.org/~doug/shootout/

class FIBO

creation make

feature

make is

local
n: INTEGER;
do
if argument_count = 1 then
n := argument(1).to_integer
else
n := 1
end
std_output.put_integer(fib(n))
std_output.put_new_line
end;

feature {NONE}

fib(n:INTEGER):INTEGER is
do
if n < 2 then
Result := 1
else
Result := fib(n-2) + fib(n-1)
end
end;
end
```
fibo.slang
```% \$Id: fibo.slang,v 1.0 2002/11/26 10:33:00 dada Exp \$
%
% contributed by John E. Davis

define fib ();
define fib(n)
{
if (n < 2)
return 1;
fib(n-2) + fib(n-1);
}
vmessage ("%d", fib(integer (__argv[1])));
```
fibo.smlnj
```(* -*- mode: sml -*-
* \$Id: fibo.smlnj,v 1.5 2001/07/09 00:25:28 doug Exp \$
* http://www.bagley.org/~doug/shootout/
*)

structure Test : sig
val main : (string * string list) -> OS.Process.status
end = struct

fun fib 0 = 1
| fib 1 = 1
| fib n = fib (n-2) + fib (n-1);

fun atoi s = case Int.fromString s of SOME num => num | NONE => 0;

fun main(name, args) =
let
val arg = hd(args @ ["1"]);
val num = atoi arg;
val fib = fib num;
val result = Int.toString fib;
in
print result; print "\n";
OS.Process.success
end
end

val _ = SMLofNJ.exportFn("fibo", Test.main);
```
fibo.tcl
```#!/usr/local/bin/tclsh
# \$Id: fibo.tcl,v 1.4 2000/12/30 17:35:51 doug Exp \$
# http://www.bagley.org/~doug/shootout/

# with help from: Kristoffer Lawson

proc fib {n} {
if {\$n < 2} {
return 1
} else {
return [expr {[fib [expr {\$n-2}]] + [fib [expr {\$n-1}]]}]
}
}

set N [lindex \$argv 0]
if {\$N < 1} { set N 1 }
puts [fib \$N]
```
fibo.vbscript
```Function Fib(N)
If N < 2 Then
Fib = 1
Else
Fib = Fib(N-2) + Fib(N-1)
End If
End Function

NUM = WScript.Arguments(0)
If NUM < 1 Then NUM = 1
nfib = Fib(NUM)
WScript.Echo nfib
```
fibo.vc
```/* -*- mode: c -*-
* \$Id: fibo.gcc,v 1.2 2000/12/24 19:10:50 doug Exp \$
* http://www.bagley.org/~doug/shootout/
*/

#include <stdio.h>
#include <stdlib.h>

unsigned long
fib(unsigned long n) {
if (n < 2)
return(1);
else
return(fib(n-2) + fib(n-1));
}

int
main(int argc, char *argv[]) {
int N = ((argc == 2) ? atoi(argv[1]) : 1);
printf("%ld\n", fib(N));
return(0);
}
```
fibo.vc++
```// -*- mode: c++ -*-
// \$Id: fibo.g++,v 1.3 2001/06/20 03:20:02 doug Exp \$
// http://www.bagley.org/~doug/shootout/

#include <iostream>
#include <stdlib.h>

using namespace std;

unsigned long fib(unsigned long n) {
if (n < 2)
return(1);
else
return(fib(n-2) + fib(n-1));
}

int main(int argc, char *argv[]) {
int n = ((argc == 2) ? atoi(argv[1]) : 1);

cout << fib(n) << endl;
return(0);
}
```
fibo.vpascal
```program fibo;
uses SysUtils;

function fib(N : integer) : integer;
begin
if N < 2 then fib := 1
else fib := fib(N-2) + fib(N-1);
End;

var NUM, f : integer;

begin
if ParamCount = 0 then
NUM := 1
else
NUM := StrToInt(ParamStr(1));

if NUM < 1 then NUM := 1;
f := fib(NUM);
WriteLn( IntToStr(f) );
end.
```