Permalink
Browse files

2 Project Euler problems

  • Loading branch information...
1 parent 49843a5 commit 5358aa4ff8c71d4cb00bda85977e932ea1887602 @credmp committed Mar 6, 2013
@@ -1,8 +1,8 @@
---
tags: [Erlang, Project Euler]
title: "Project Euler Problem 1"
-image: /assets/post_images/project-euler.png
-image_credit: Project Euler
+image: /assets/post_images/Puzzle.JPG
+image_credit: Unknown
layout: post
---
@@ -0,0 +1,41 @@
+---
+tags: [Erlang, Project Euler]
+title: "Project Euler Problem 2"
+image: /assets/post_images/Puzzle.JPG
+image_credit: Unknown
+layout: post
+---
+
+Problem
+-------
+
+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, ...
+
+By considering the terms in the Fibonacci sequence whose values do
+not exceed four million, find the sum of the even-valued terms.
+
+
+Solution
+--------
+
+{% highlight erlang %}
+-module(problem2).
+-export([main/0]).
+
+solve(Max, Cur, Prev, Acc) ->
+ if
+ Cur > Max ->
+ Acc;
+ Cur rem 2 == 0 ->
+ solve(Max, Cur + Prev, Cur, Acc+Cur);
+ true ->
+ solve(Max, Cur + Prev, Cur, Acc)
+ end.
+
+main() ->
+ solve(4000000, 1, 1, 0).
+{% endhighlight %}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5358aa4

Please sign in to comment.