Sha256: 5b3b214524b311d4e6e4d80d3e64bb6094b69386f984edd0ec1c08ba6486605a

Contents?: true

Size: 571 Bytes

Versions: 69

Compression:

Stored size: 571 Bytes

Contents

defmodule CollatzConjecture do
  @doc """
  calc/1 takes number (> 1), and returns the number of steps required to get to 1 when
  following the rules: if n is odd, multiply with 3 and add 1. if n is even, divide by 2
  """
  @spec calc(number :: pos_integer) :: pos_integer
  def calc(number) when number > 0 and is_integer(number) do
    calc(number, 0)
  end

  defp calc(1, steps), do: steps

  defp calc(number, steps) when rem(number, 2) == 0 do
    calc(div(number, 2), steps + 1)
  end

  defp calc(number, steps) do
    calc(number * 3 + 1, steps + 1)
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/collatz-conjecture/example.exs