Skip to content

Instantly share code, notes, and snippets.

@tryperl
Created March 4, 2013 03:35
Show Gist options
  • Save tryperl/5079721 to your computer and use it in GitHub Desktop.
Save tryperl/5079721 to your computer and use it in GitHub Desktop.
Created by www.tryperl.com.
#http://projecteuler.net/problem=2
#Each new term in the Fibonacci sequence is generated by adding the previous two terms.
#By starting with 1 and 2, the first 10 terms will be:
#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
#By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
use strict;
use warnings;
my $a = 0;
my $b = 1;
my $n = 0;
my $sum = 0;
while($n < 4000000)
{
$n = $a + $b;
if(($n % 2) == 0)
{
$sum += $n;
}
$a = $b;
$b = $n;
}
print $sum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment