programming-examples/perl/Subroutine/calculate 1000th element of standard Fibonacci sequence.pl
2019-11-15 12:59:38 +01:00

24 lines
496 B
Perl

#!/usr/bin/perl
use warnings;
use strict;
sub fibonacci3 {
my ($count, $aref) = @_;
unless ($aref) {
# first call - initialize
$aref = [1,1];
$count -= scalar(@{$aref});
}
if ($count--) {
my $next = $aref->[-1] + $aref->[-2];
push @{$aref}, $next;
@_ = ($count, $aref);
goto &fibonacci3;
} else {
return wantarray?@{$aref}:$aref->[-1];
}
}
print scalar(fibonacci3(1000)), "\n";