Skip to content

Instantly share code, notes, and snippets.

@adamwiggall
Last active August 13, 2016 01:37
Show Gist options
  • Save adamwiggall/08b3b6e168d7b34b603d4ee406b15615 to your computer and use it in GitHub Desktop.
Save adamwiggall/08b3b6e168d7b34b603d4ee406b15615 to your computer and use it in GitHub Desktop.
defmodule Collatz do
def conjecture(int) when int < 2 do
raise ArgumentError, message: "Requires an integer value greater than 1"
end
def conjecture(int), do: conjecture(int, 0)
defp conjecture(1, 1), do: "Reduced to 1 in 1 step"
defp conjecture(1, steps), do: "Reduced to 1 in #{steps} steps"
defp conjecture(int, steps) when rem(int, 2) == 0 do
conjecture(div(int, 2), steps + 1)
end
defp conjecture(int, steps) do
conjecture(3 * int + 1, steps + 1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment