Sha256: e5cb4eb00c9c37bb1e8e24738e6d09ca0df48205ed761e5cf5f2999023ba3386

Contents?: true

Size: 571 Bytes

Versions: 124

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

124 entries across 124 versions & 1 rubygems

Version Path
trackler-2.2.1.109 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.108 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.107 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.106 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.105 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.104 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.103 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.102 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.101 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.100 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.99 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.98 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.97 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.96 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.95 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.94 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.93 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.92 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.91 tracks/elixir/exercises/collatz-conjecture/example.exs
trackler-2.2.1.90 tracks/elixir/exercises/collatz-conjecture/example.exs