#!/usr/bin/env perl # Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) # # This work is licensed under the Creative Commons Attribution-NonCommercial- # ShareAlike License. To view a copy of this license, visit # http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to # Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, # 94105, USA. sub fibonacci ($) { my $n = shift; my ($fib, $prev_fib) = (1,1); my $i; return -1 if ($n <= 0); for ($i = 2; $i < $n; $i++) { ($fib, $prev_fib) = ($fib + $prev_fib, $fib); } return $fib; } if ($#ARGV != 0) { print stderr "Usage:\n\t$0 (n-th fibonacci # to calculate)\n"; exit -1; } $n = $ARGV[0]; print "fibonacci($n): ".fibonacci($n)."\n";