#!/usr/bin/perl -w
######
#
# Recursion demo with fib series : 0, 1, 1, 2, 3, 5, 8, 13, 21
#
#####
use strict;

# Calculate a term in the fibonnaci series recursively.
# Usage: $x = fib(4);   # returns the 4th fibonnaci number.
sub fib {
  my ($i, $tab) = @_;
  print "$tab fib($i)\n";
  if ($i==0){
    return 0;
  }
  elsif ($i==1){
    return 1;
  }
  else {
    my $more_tab = $tab . "  ";
    return fib($i-1, $more_tab) + fib($i-2, $more_tab);
  }
}

# ---------------------------------------------------

print " the 5th fibonnaci number is " , fib(5, "") , "\n";


