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:
Haskell:
--fibinaci :: (Num a) => a -> a -> [a] fibinaci a b
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; } }
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
Bad Python
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:
How's about this?
fibo is a closure
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;
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;