Skip to content

Instantly share code, notes, and snippets.

@MaroShim
Created November 1, 2016 08:02
Show Gist options
  • Save MaroShim/7560ca51cc4d68e90cf0fa2aa71286bd to your computer and use it in GitHub Desktop.
Save MaroShim/7560ca51cc4d68e90cf0fa2aa71286bd to your computer and use it in GitHub Desktop.
# sum of fibonacci numbers which is even and less than 4000000
# https://elixirforum.com/t/project-euler-problem-2/500/2
defmodule Problem2 do
def fib_seq do
Stream.unfold({1,1}, fn {a,b} -> {a, {b, a + b}} end)
end
def solve(limit) do
fib_seq
|> Stream.take_while(&(&1 < limit))
|> Stream.filter(&(rem(&1, 2) == 0))
|> Enum.sum
end
def print do
IO.puts solve(4000000)
end
end
Problem2.print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment