19 lines
608 B
Elixir
19 lines
608 B
Elixir
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
|