Fibonacci to n

By Ziddykins on Jun 02, 2014

Calculates Fibonacci to the limit you specify.
Can calculate fib(500) in roughly half a second.
You'll need the Math::BigInt module. As far as I know it comes default, but if you don't have it, use cpan
"sudo cpan Math::BigInt"

use warnings; use strict;
use Math::BigInt;

my $n = 2; my $a;
my @nums = qw(0 1);

print "Calculate Fibonacci sequence to [number]: ";
my $max = <STDIN>;

while ($n < $max) {
    $a = Math::BigInt->new($nums[$n-1] + $nums[$n-2]);
    push(@nums, $a);
    printf("%s\n", $a);


Sign in to comment.
Hawkee   -  Oct 02, 2014

I've had this come up in a developer interview before, so it's something to be aware of. Just don't forget the lower bounds!

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.