Elixir:
defmodule ShortestStep do import Integer @spec steps(non_neg_integer) :: non_neg_integer def steps(start) when start > 0, do: do_steps(start) @spec do_steps(non_neg_integer) :: non_neg_integer defp do_steps(1), do: 0 defp do_steps(current) when is_odd(current), do: do_steps(current - 1) + 1 defp do_steps(current), do: do_steps(div(current, 2)) + 1 end
Recursive thinking is fun :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Elixir:
Recursive thinking is fun :)