Created
November 1, 2016 08:02
-
-
Save MaroShim/7560ca51cc4d68e90cf0fa2aa71286bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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