Browse Source

2nd euler thingy

Brennen Bearnes 8 years ago
parent
commit
65585f79b5
1 changed files with 43 additions and 0 deletions
  1. 43
    0
      euler/2.pl

+ 43
- 0
euler/2.pl View File

@@ -0,0 +1,43 @@
1
+#!/usr/bin/perl 
2
+
3
+=pod
4
+
5
+=head1 NAME
6
+
7
+2.pl
8
+
9
+=head1 SYNOPSIS
10
+
11
+perl ./2.pl 
12
+
13
+=head1 DESCRIPTION
14
+
15
+Project Euler, Problem 2:
16
+
17
+Each new term in the Fibonacci sequence is generated by adding the previous two
18
+terms. By starting with 1 and 2, the first 10 terms will be:
19
+
20
+1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
21
+
22
+Find the sum of all the even-valued terms in the sequence which do not exceed
23
+four million.
24
+
25
+=head1 AUTHOR
26
+
27
+Brennen Bearnes <bbearnes@gmail.com>
28
+http://p1k3.com/
29
+
30
+=cut
31
+
32
+use strict;
33
+use warnings;
34
+use 5.10.0;
35
+
36
+my ($sum, $prev, $cur) = (0, 0, 1);
37
+while ($cur < 4e6) {
38
+  $sum += $cur if $cur =~ /[02468]$/;
39
+  my $tmp = $cur;
40
+  $cur += $prev;
41
+  $prev = $tmp;
42
+}
43
+say $sum;

Loading…
Cancel
Save