I've got problem #2 in the bag.

Problem #2 goes like this:

1 2 3 4 5 6 7 | ```
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, ...
Find the sum of all the even-valued terms in the sequence which do not
exceed four million.
``` |

And my code goes as follows:

Haskell:

1 2 3 4 5 6 7 | ```
--fibinaci :: (Num a) => a -> a -> [a]
fibinaci a b
| sum' > 4000000 = []
| otherwise = sum' : fibinaci b sum'
where sum' = a + b
problem2 = sum $ filter (even) (fibinaci 0 1)
``` |

Python:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | ```
#!/usr/bin/python
def fibinaci_list( in_list, input1, input2):
flip = True
while ( input1 + input2 < 4000000):
if( flip == True):
input1 = input1 + input2
in_list.append(input1)
flip = False
else:
input2 = input1 + input2
in_list.append(input2)
flip = True
def even(n):
if n % 2 == 0:
return True
else:
return False
def main():
first_list = []
fibinaci_list(first_list, 0, 1)
second_list = filter(even, first_list)
print "%s" % (sum(second_list))
if __name__ == "__main__":
main()
``` |

And finally, Perl:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | ```
#!/usr/bin/perl
my $total = 0;
my $count1 = 0;
my $count2 = 1;
my $flip = 1;
while( $count2 + $count1 < 4000000)
{
if ( $flip == 1)
{
$count1 += $count2;
if ( $count1 % 2 == 0)
{
$total += $count1;
}
$flip = 0;
}
else
{
$count2 += $count1;
if ( $count2 % 2 == 0)
{
$total += $count2;
}
$flip = 1;
}
}
print $total;
``` |

Originally, I tried to copy the Python algorithm to Perl, using an array instead of a list. That didn't work. The program would always fail due to out of memory errors. To solve this problem I changed the program to work without using arrays and just took the whole fibinaci function out and put it into main.

As always, constructive comments for the code are welcomed.

## Comments !