How many Fibonacci numbers?    

Recursive algorithm coded in Perl

Courtesy of Wikipedia Generating $n Fibonacci numbers:
####################################
for ($i=1; $i<=$n; $i++)
{
     $fibonacci = fib($i);
     print "$fibonacci\n";
}
######################
# FUNCTION DEFINITION
sub fib
{
     # Get the function argument ($i)
     my $i = shift;

     # The so called base for $i=1 or $i=2
     if ($i < 3)
     {
          my $fibonacci = $i-1;
          return $fibonacci;
     }
     else
     {
          # Recursive call of the fib function!!!
          my $fibonacci = (fib($i-1) + fib($i-2));
          return $fibonacci;
     }
}
####################################

A recursive algorithm (note: the above can be much optimized, that's for sure) should theoretically be able to generate the sequence of 1477 Fibonacci numbers starting from 0, however, it would literally take for ages as the algorithm is extremely time-consuming and CPU-intensive as compared to iterative or arithmetic methods.

For example, the generation of 35 Fibonacci numbers using the above recursive algorithm (virtual machine box with Windows 10 x64, Intel Core i7-6700 CPU dual-core, 8 GB RAM) takes as long as 30 seconds! Of course, my WWW server running on a more powerful Linux unit should be slightly faster than that. Feel free to go up and give it a go!

Not surprisingly, the iterative algorithm will do the same thing in much less than a second!!! cool