My Fibonacci sequence as a recursive function is an infinite loop
The following function recurses infinitely and I don't see why. It enters the conditional statements but doesn't seem to be terminating with the return statement. use strict; use warnings; print fibonacci(100); sub fibonacci { my $number = shift; if ($number == 0) { print "return 0\n"; return 0; } elsif ($number == 1) { print "return 1\n"; return 1; } else { return fibonacci($number-1) + fibonacci($number-2); } } Your loop does not recurse infinitely, it just takes way too long with an input of 100. Try a memoized version: { my @fib; sub fib { my $n = shift; return $fib[$n] if defined $fib[$n]