Add several elixir solutions
This commit is contained in:
18
elixir/lucas-numbers/lib/lucas_numbers.ex
Normal file
18
elixir/lucas-numbers/lib/lucas_numbers.ex
Normal file
@@ -0,0 +1,18 @@
|
||||
defmodule LucasNumbers do
|
||||
defguardp is_positive(k) when is_integer(k) and k > 0
|
||||
@moduledoc """
|
||||
Lucas numbers are an infinite sequence of numbers which build progressively
|
||||
which hold a strong correlation to the golden ratio (φ or ϕ)
|
||||
|
||||
E.g.: 2, 1, 3, 4, 7, 11, 18, 29, ...
|
||||
"""
|
||||
def generate(1), do: [2]
|
||||
def generate(2), do: [2, 1]
|
||||
def generate(count) when is_positive(count) do
|
||||
Stream.iterate({2,1}, fn {x,y} -> {y, x+y} end)
|
||||
|> Stream.map(fn {x,_} -> x end)
|
||||
|> Enum.take(count)
|
||||
end
|
||||
|
||||
def generate(_), do: raise ArgumentError, "count must be specified as an integer >= 1"
|
||||
end
|
Reference in New Issue
Block a user