Last active
August 13, 2016 01:37
-
-
Save adamwiggall/08b3b6e168d7b34b603d4ee406b15615 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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