summaryrefslogblamecommitdiffstats
path: root/002.pl
blob: 6b59aed35ea36451bc9eb45ce846586264e20b3e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

















                                                            
#!/usr/bin/perl
use strict;
use warnings;

my $root5 = sqrt 5;
my $phi = (1 + $root5)/2;

sub fib { int (($phi**$_[0]-(1-$phi)**$_[0])/$root5 + 0.5) }

my $i = 0;
my $sum = 0;
while (1) {
    $i++;
    my $n = fib $i;
    last if $n > 1000000;
    $sum += $n if $n % 2 == 0;
}
print "$sum\n";