Solved project euler 2
authorDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 13 Dec 2016 14:45:38 +0000 (09:45 -0500)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 13 Dec 2016 14:45:38 +0000 (09:45 -0500)
euler/elixir/0002-sum-of-fibonacci.exs [new file with mode: 0644]

diff --git a/euler/elixir/0002-sum-of-fibonacci.exs b/euler/elixir/0002-sum-of-fibonacci.exs
new file mode 100644 (file)
index 0000000..350ef01
--- /dev/null
@@ -0,0 +1,20 @@
+defmodule SumOfEvenFibonacci do
+  def fibonacci_stream() do
+    items_with_prev = Stream.iterate({0,1}, fn({prev, curr}) -> {curr, prev + curr} end)
+    Stream.map(items_with_prev, fn({_, curr}) -> curr end)
+  end
+
+  def fibonacci_to(limit) do
+    Stream.take_while(fibonacci_stream(), fn(n) -> n < limit end)
+  end
+
+  def filter_even(stream) do
+    Stream.filter(stream, fn(n) -> rem(n, 2) == 0 end)
+  end
+
+  def even_fibonacci_to(limit) do
+    filter_even(fibonacci_to(limit))
+  end
+end
+
+IO.inspect(Enum.sum(SumOfEvenFibonacci.even_fibonacci_to(4_000_000)))