You can make the code more concise and speed up massively by saving it.
A module Memoizeis the easiest and clearest way to memoize (cache results) a routine.
. - , , , , .
use strict;
use warnings;
use Memoize;
memoize('fibonacci');
print fibonacci(100);
sub fibonacci {
my ($n) = @_;
$n < 2 ? $n : fibonacci($n-1) + fibonacci($n-2);
}
Deep recursion on anonymous subroutine at E:\Perl\source\fib.pl line 11.
Deep recursion on anonymous subroutine at E:\Perl\source\fib.pl line 11.
3.54224848179262e+020