I want to find the prime factors of a number. I wrote:
defmodule Prime do
def factors(n) when is_integer(n) and n > 1 do
Stream.unfold(n, fn 1 -> nil; n -> &({&1, div(n, &1)}).(lf n) end) |> Enum.to_list
end
def lf(n) when is_integer(n) and n > 1 do
if rem(n, 2) == 0 do 2
else Stream.iterate(3, &(&1 + 2)) |> Stream.filter(&(rem(n, &1) == 0))
|> Stream.take(1) |> Enum.to_list |> hd
end
end
end
The problem lies at line 3: in the anonymous function I have to return the tuple {f, n / f}, where f is the lowest factor of n and is calculated in the lf function. However, I don't want to compute it twice (I could do {lf n, div(n, lf n)}). I also tried to define a closure, but apparently it's not allowed.
The trick that I used doesn't even work.
How is it done?