about summary refs log tree commit diff
path: root/users/wpcarro/assessments/semiprimes/server/lib/math.ex
blob: 8a33be475389ad640bcffa19ed95c779687c9b70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
defmodule Math do
  @moduledoc """
  Math utilities.
  """
  alias Extras

  @doc """
  Returns the prime factors for `n`.

  ## Examples

      iex> Math.factor(15)
      [3, 5]

  """
  def factor(1), do: []

  def factor(n) do
    Extras.range(2, n - 1)
    |> Enum.find(&(rem(n, &1) == 0))
    |> case do
      nil -> [n]
      x -> [x | factor(div(n, x))]
    end
  end
end