Fibonacci number in Perl

Inspired by Brian here are the Fibonacci numbers in Perl.

sub fib {
    my ($n) = @_;
    my $phi = (1 + sqrt(5)) / 2;
    return int ((($phi ** $n) - ((1 - $phi) ** $n) ) / sqrt(5));
}

for (0..20) {
  print fib($_)."\n";
}

Comments

One Response to “Fibonacci number in Perl”

  1. Fibonacci Sequence Illustrated by Nature : Broerse Blog on May 16th, 2009 5:51 pm

    […] Link & Fibonacci number in Perl […]