#!/usr/bin/perl # Latest perl version with the newest features and bugfixes use v5.40; # Do all kinds of automatic checking of the program code use strict; use warnings; # Great debugging tool to quickly print a complex structure (array, hash, scalar, whatever) to the console use Data::Dumper; # Call the calculation main routine my $max = 100; # 4_000_000; my @baseframe = (1, 2, 3); my $result = fib($max, \@baseframe); # Call with a REFERENCE to @baseframe, so subs can modify it # Print the final result print 'Final: ', $result, "\n"; # For debugging, print the final state of @baseframe print 'Final @baseframe: ', Dumper(\@baseframe), "\n"; # And end scene... exit(0); # This does ONE round of the calculation, no deep recursion sub fib_one_round($frame) { $frame->[0] = $frame->[0] + $frame->[1]; $frame->[1] = $frame->[2]; $frame->[3] = $frame->[2]; if(($frame->[0] %2) != 0) { return $frame->[0]; } return 0; } # This does the main loop. We are working on an array *reference*, so fib_one_round can modify the array sub fib($max, $frame = [1, 2, 3]) { my $result = 0; # Loop until the max value has been reached while($result < $max) { $result += fib_one_round($frame); # Print the intermediate results for debugging print $result, "\n"; } return $result; }