сollatz-сonjecture
This commit is contained in:
parent
4c3da6abe4
commit
b62fcbc5ea
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"authors": [
|
||||
"Tuxified"
|
||||
],
|
||||
"contributors": [
|
||||
"angelikatyborska",
|
||||
"Cohen-Carlisle",
|
||||
"devonestes",
|
||||
"neenjaw",
|
||||
"sotojuan"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lib/collatz_conjecture.ex"
|
||||
],
|
||||
"test": [
|
||||
"test/collatz_conjecture_test.exs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.ex"
|
||||
]
|
||||
},
|
||||
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
|
||||
"source": "An unsolved problem in mathematics named after mathematician Lothar Collatz",
|
||||
"source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"collatz-conjecture","id":"ccd0e87e9b8944838d567e25ffb84d23","url":"https://exercism.org/tracks/elixir/exercises/collatz-conjecture","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
|
@ -0,0 +1,24 @@
|
|||
# The directory Mix will write compiled artifacts to.
|
||||
/_build/
|
||||
|
||||
# If you run "mix test --cover", coverage assets end up here.
|
||||
/cover/
|
||||
|
||||
# The directory Mix downloads your dependencies sources to.
|
||||
/deps/
|
||||
|
||||
# Where third-party dependencies like ExDoc output generated docs.
|
||||
/doc/
|
||||
|
||||
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||
/.fetch
|
||||
|
||||
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||
erl_crash.dump
|
||||
|
||||
# Also ignore archive artifacts (built via "mix archive.build").
|
||||
*.ez
|
||||
|
||||
# Ignore package tarball (built via "mix hex.build").
|
||||
collatz_conjecture-*.tar
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
From the terminal, change to the base directory of the exercise then execute the tests with:
|
||||
|
||||
```bash
|
||||
$ mix test
|
||||
```
|
||||
|
||||
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
|
||||
|
||||
Documentation:
|
||||
|
||||
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
|
||||
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
|
||||
|
||||
## Pending tests
|
||||
|
||||
In test suites of practice exercises, all but the first test have been tagged to be skipped.
|
||||
|
||||
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.
|
||||
|
||||
For example:
|
||||
|
||||
```elixir
|
||||
# @tag :pending
|
||||
test "shouting" do
|
||||
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
|
||||
end
|
||||
```
|
||||
|
||||
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
|
||||
|
||||
```bash
|
||||
$ mix test --include pending
|
||||
```
|
||||
|
||||
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
|
||||
|
||||
```elixir
|
||||
# ExUnit.configure(exclude: :pending, trace: true)
|
||||
```
|
||||
|
||||
## Useful `mix test` options
|
||||
|
||||
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
|
||||
* `--failed` - runs only tests that failed the last time they ran
|
||||
* `--max-failures` - the suite stops evaluating tests when this number of test failures
|
||||
is reached
|
||||
* `--seed 0` - disables randomization so the tests in a single file will always be ran
|
||||
in the same order they were defined in
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit lib/collatz_conjecture.ex` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
|
||||
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.
|
|
@ -0,0 +1,52 @@
|
|||
# Collatz Conjecture
|
||||
|
||||
Welcome to Collatz Conjecture on Exercism's Elixir Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
The Collatz Conjecture or 3x+1 problem can be summarized as follows:
|
||||
|
||||
Take any positive integer n.
|
||||
If n is even, divide n by 2 to get n / 2.
|
||||
If n is odd, multiply n by 3 and add 1 to get 3n + 1.
|
||||
Repeat the process indefinitely.
|
||||
The conjecture states that no matter which number you start with, you will always reach 1 eventually.
|
||||
|
||||
Given a number n, return the number of steps required to reach 1.
|
||||
|
||||
## Examples
|
||||
|
||||
Starting with n = 12, the steps would be as follows:
|
||||
|
||||
0. 12
|
||||
1. 6
|
||||
2. 3
|
||||
3. 10
|
||||
4. 5
|
||||
5. 16
|
||||
6. 8
|
||||
7. 4
|
||||
8. 2
|
||||
9. 1
|
||||
|
||||
Resulting in 9 steps.
|
||||
So for input n = 12, the return value would be 9.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @Tuxified
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @angelikatyborska
|
||||
- @Cohen-Carlisle
|
||||
- @devonestes
|
||||
- @neenjaw
|
||||
- @sotojuan
|
||||
|
||||
### Based on
|
||||
|
||||
An unsolved problem in mathematics named after mathematician Lothar Collatz - https://en.wikipedia.org/wiki/3x_%2B_1_problem
|
|
@ -0,0 +1,15 @@
|
|||
defmodule CollatzConjecture do
|
||||
@doc """
|
||||
calc/1 takes an integer and returns the number of steps required to get the
|
||||
number to 1 when following the rules:
|
||||
- if number is odd, multiply with 3 and add 1
|
||||
- if number is even, divide by 2
|
||||
"""
|
||||
@spec calc(input :: pos_integer()) :: non_neg_integer()
|
||||
def calc(input) when is_integer(input) and input > 0, do: collatz(input)
|
||||
|
||||
defp collatz(input, steps \\ 0)
|
||||
defp collatz(1, steps), do: steps
|
||||
defp collatz(input, steps) when rem(input, 2) == 0, do: collatz(div(input, 2), steps + 1)
|
||||
defp collatz(input, steps), do: collatz(input * 3 + 1, steps + 1)
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
defmodule CollatzConjecture.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :collatz_conjecture,
|
||||
version: "0.1.0",
|
||||
# elixir: "~> 1.8",
|
||||
start_permanent: Mix.env() == :prod,
|
||||
deps: deps()
|
||||
]
|
||||
end
|
||||
|
||||
# Run "mix help compile.app" to learn about applications.
|
||||
def application do
|
||||
[
|
||||
extra_applications: [:logger]
|
||||
]
|
||||
end
|
||||
|
||||
# Run "mix help deps" to learn about dependencies.
|
||||
defp deps do
|
||||
[
|
||||
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
]
|
||||
end
|
||||
end
|
|
@ -0,0 +1,39 @@
|
|||
defmodule CollatzConjectureTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "zero steps for one" do
|
||||
assert CollatzConjecture.calc(1) == 0
|
||||
end
|
||||
|
||||
test "zero is an error" do
|
||||
assert_raise FunctionClauseError, fn -> CollatzConjecture.calc(0) end
|
||||
end
|
||||
|
||||
test "divide if even" do
|
||||
assert CollatzConjecture.calc(16) == 4
|
||||
end
|
||||
|
||||
test "even and odd steps" do
|
||||
assert CollatzConjecture.calc(12) == 9
|
||||
end
|
||||
|
||||
test "Large number of even and odd steps" do
|
||||
assert CollatzConjecture.calc(1_000_000) == 152
|
||||
end
|
||||
|
||||
test "start with odd step" do
|
||||
assert CollatzConjecture.calc(21) == 7
|
||||
end
|
||||
|
||||
test "more steps than starting number" do
|
||||
assert CollatzConjecture.calc(7) == 16
|
||||
end
|
||||
|
||||
test "negative value is an error " do
|
||||
assert_raise FunctionClauseError, fn -> CollatzConjecture.calc(-15) end
|
||||
end
|
||||
|
||||
test "string as input value is an error " do
|
||||
assert_raise FunctionClauseError, fn -> CollatzConjecture.calc("fubar") end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true)
|
Loading…
Reference in New Issue