Fibonacci in Twelve Languages

Displaying the first 22 fibonacci numbers in twelve programming languages that are available in Ubuntu Linux 11.04.  This code is non-recursive.

C

# vi fib.c

#include <stdio.h>

int main(void) {
  int iCount;

  int a;
  int b;
  int c;

  iCount = 0;
  a = 0;
  b = 1;

  printf("%d\n", a);
  printf("%d\n", b);

  while (iCount < 20) {
    c = a + b;
    printf("%d\n", c);
    a = b;
    b = c;
    iCount++;
  }

  return 0;
}

# gcc fib.c -o fib

# ./fib

C++

# vi fib.cpp

#include <iostream>
using namespace std;

int main(void) {
  int iCount;
  int a;
  int b;
  int c;

  iCount = 0;
  a = 0;
  b = 1;

  cout << a << endl;
  cout << b << endl;

  while (iCount < 20) {
    c = a + b;
    cout << c << endl;
    a = b;
    b = c;
    iCount++;
  }

  return 0;
}

# g++ fib.cpp -o fib

# ./fib

Java

# vi Fib.java

public class Fib {

  public Fib() {
    int iCount;

    int a;
    int b;
    int c;

    iCount = 0;
    a = 0;
    b = 1;

    System.out.println(a + "");
    System.out.println(b + ""); 

    while (iCount < 20) {
      c = a + b;
      System.out.println(c + "");
      a = b;
      b = c;
      iCount++;
    }
  }

  public static void main(String args[]) {
    new Fib();
  }
}

# javac Fib.java

# java Fib

Ruby

# vi fib.rb

a = 0
b = 1
iCount = 0

puts "#{a}"
puts "#{b}"

while (iCount < 20)
  c = a + b
  puts "#{c}"
  a = b;
  b = c;
  iCount += 1

end

# ruby fib.rb

Perl

# vi fib.pl

$a = 0;
$b = 1;
$iCount = 0;

print $a . "\n";
print $b . "\n";

while ($iCount < 20) {
  $c = $a + $b;
  print $c . "\n";
  $a = $b;
  $b = $c;
  $iCount++;
}

# perl fib.pl

SmallTalk (Squeak)

| a b c iCount |

a := 0.
b := 1.
iCount := 0.

Transcript show: a.
Transcript cr.
Transcript show: b.
Transcript cr.

[iCount < 20] whileTrue: [
  c := a + b.
  Transcript show: c.
  Transcript cr.
  a := b.
  b := c.
  iCount := iCount + 1.
]

Python

# vi fib.py

a = 0
b = 1
iCount = 0

print a
print b

while iCount < 20:
  c = a + b
  print c
  a = b
  b = c
  iCount += 1

# python fib.py

FORTRAN 77

# vi fib.f

      program fib
      integer a, b, c, iCount

      a = 0
      b = 1

      write(*, *) a
      write(*, *) b

      iCount = 0

      do iCount=1,20
        c = a + b
        write(*, *) c
        a = b
        b = c
      end do

      stop
      end

# f77 fib.f -o fib

# gfortran fib.f -o fib

# ./fib

Scheme

# vi fib.scm

(define a 0)
(define b 1)
(display a) (newline)
(display b) (newline)
(define c 0)
(let loop ((iCount 1))
  (if (<= iCount 20)
      (begin
        (set! c (+ a b))
        (display c) (newline)
        (set! a b)
        (set! b c)
        (loop (+ iCount 1)))))

# scheme –load fib.scm

Scala

# vi Fib.scala

object Fib {

  def display() = {
    var a = 0
    var b = 1
    var c = 0

    println(a)
    println(b)

    var iCount = 0

    while (iCount < 20){
      c = a + b
      println(c )
      a = b
      b = c
      iCount += 1
    }

  }

  def main(args: Array[String]) {
    this.display()
  }
}

# scalac Fib.scala

# scala Fib

PHP

# vi fib.php

<?php
$a = 0;
$b = 1;

echo $a . "\n";
echo $b . "\n";

$iCount = 0;
while ($iCount < 20) {
  $c = $a + $b;
  echo $c . "\n";
  $a = $b;
  $b = $c;
  $iCount++;
}

?>

# php fib.php

bash script

# vi fib.sh

#!/bin/bash
a=0
b=1

echo $a
echo $b

iCount=0

while [ $iCount -lt 20 ]
do
  c=$(( b + a ))
  echo $c
  a=$(( b ))
  b=$(( c ))
  (( iCount++ ))

done

# chmod u+x fib.sh

# ./fib.sh

Output

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
Advertisements