Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Untitled notebook

happy_number.livemd

Untitled notebook

Section

defmodule Solution do
  @map %{
    9 => false,
    8 => false,
    7 => true,
    6 => false,
    5 => false,
    4 => false,
    3 => false,
    2 => false,
    1 => true
  }
  @spec is_happy(n :: integer) :: boolean
  def is_happy(n) when n < 9, do: Map.get(@map, n)

  def is_happy(n) do
    n
    |> to_num_list
    |> Enum.map(&amp;:math.pow(&amp;1, 2))
    |> Enum.reduce(&amp;(&amp;1 + &amp;2))
    |> round()
    |> is_happy()
  end

  def to_num_list(n),
    do: n |> to_string() |> String.split("", trim: true) |> Enum.map(&amp;String.to_integer(&amp;1))
end

Solution.is_happy(4)