Skip to content

Instantly share code, notes, and snippets.

@djacobs
Created November 10, 2011 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djacobs/1356267 to your computer and use it in GitHub Desktop.
Save djacobs/1356267 to your computer and use it in GitHub Desktop.
Solving Euler problems with @jakedobkin
use strict;
#use bigint;
use constant TARGET => 20;
use constant VERBOSE => 1;
my $total = 0;
my %fib;
sub fib($)
{
my $num = shift;
($num<=1) and return(0);
($num==2) and return(1);
(VERBOSE > 2) and print "looking at $num...\n";
if ($fib{$num} > 0) {
(VERBOSE > 2) and print "We already knew that the ", $num, "th number in the sequence was", $fib{$num}, "\n";
return ($fib{$num});
}
$fib{$num} = (fib($num-1) + fib($num-2));
(VERBOSE > 1) and print "Looks like the fibonacci number for $num is ", $fib{$num}, "\n";
unless ($fib{$num} % 2) {
$total += $fib{$num};
VERBOSE and print "Current total of evens is $total after adding the ", $num, "th number in the sequence is, ", $fib{$num}, "\n";
}
return ($fib{$num});
}
print "Current total of evens is $total\n";
print "fibonacci ", TARGET, " is ", fib(TARGET), "\n";
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment