## Project Euler, Problem 2

I've got problem #2 in the bag.

Problem #2 goes like this:
`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:

`--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:
`#!/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:
`#!/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.

### my python version

This is what I used to solve. Idk how fast it is

`a, b = 0, 1c = 0while b < 4000000:     b, a = a, a + b     if b % 2 == 0:          c = c + bprint c`

No offense, but I think the Python and Perl versions of this code are really bad, and I'm not sure storing the entire sequence is the best way to do it in Haskell, either.

In any case, enough people have posted better Perl, so here is a better Python version:

`even_sum, a, b, c = 0, 0, 1, 1while c<4000000:	if c&1 == 0: even_sum += c	a,b,c = b, a+b, b+c print(even_sum)`

`fibs = 1 : 2 : zipWith (+) fibs (tail fibs)problem2 = sum . takeWhile (<4000000) . filter even \$ fibs`

### fibo is a closure

`#! /usr/bin/perluse 5.10.0;use strict;use warnings;use List::Util qw/ sum /; my \$fib = do {    my @f = ( 1, 2 );      sub {        push @f, sum @f;         shift @f;     }   }; my \$count = 0;while ( my \$_ = &\$fib ) {     next if \$_ % 2;    my \$c = \$count + \$_;     if ( \$c < 4_000_000 ) { \$count = \$c }    else { say \$count; exit }}`

### Solomon Foster wrote it in Perl6:

say [+] (1, 1, *+* ... 4000000).grep(* !% 2)

### You write wrong in Perl

Your Perl code looks like ugly PHP, it's wrong and your Perl code (good Perl code) can't be the same like Python code, it's different languages, that uses different paradigms of programming. Try read some good books about Perl like "Intermediate Perl" + "High order Perl" in that order, and you'll discover incredible heights of Perl programming.

Despite your harsh language; I appreciate your input. I will purchase those book in the near future and soon after that I will start to write "prettier" Perl code.

### I speak English not so well,

I speak English not so well, and I do not want to offend you in any way.

The second book that i pointed is the best book on how to use recursion and closures, and this knowledge can be useful not only for Perl.

### Hey ZloyRusskiy, Would you

Hey ZloyRusskiy,

Would you use the Contact link above and send me an email with a way to contact you? I have problem 3 figured out, but I'm having serious problems with translating my Python algorithm to Perl.

Thanks.

### No offense taken and thank

No offense taken and thank you for saying that. Also I really do appreciate the book recommendations. The High Order Perl book has gotten some great reviews on Amazon, and I really look forward to going through it.

Also, thanks for the comments after this on with the code in them. Its much easier to learn efficient Perl when you already have an idea of what your trying to accomplish.

### Another one-liner: perl -E

Another one-liner:

perl -E 'for (\$a=1,\$b=2;\$s < 4_000_000; (\$a,\$b) = (\$b,\$a+\$b)) { \$s += \$a unless \$a % 2 }; say \$a'

### sorry, i have misprinted:

sorry, i have misprinted: "say \$s" must be in the end of code

### of full variant

use 5.012;
my \$sum;
for ( my (\$a,\$b) = (1,2) ; \$sum < 4_000_000 ; (\$a,\$b) = (\$b,\$a+\$b) ) {
\$sum += \$a unless \$a % 2;
};
say \$sum;

### why not use a shorter version

why not use a shorter version :-)

#!/usr/bin/perl
use List::Util qw(sum);

@f = (0,1);
push @f, sum @f[-1,-2] while (\$f[-1] < 4_000_000);
print sum grep {\$_ % 2 == 0} @f;

If you made it this far down into the article, hopefully you liked it enough to share it with your friends. Thanks if you do, I appreciate it.