Browse Source

Exercism: 3x+1

master
Julio Biason 3 years ago
parent
commit
4408bcd32a
  1. 27
      erlang/collatz-conjecture/.exercism/config.json
  2. 1
      erlang/collatz-conjecture/.exercism/metadata.json
  3. 36
      erlang/collatz-conjecture/HELP.md
  4. 51
      erlang/collatz-conjecture/README.md
  5. 30
      erlang/collatz-conjecture/rebar.config
  6. 9
      erlang/collatz-conjecture/src/collatz_conjecture.app.src
  7. 9
      erlang/collatz-conjecture/src/collatz_conjecture.erl
  8. 36
      erlang/collatz-conjecture/test/collatz_conjecture_tests.erl
  9. 2
      tracks.txt

27
erlang/collatz-conjecture/.exercism/config.json

@ -0,0 +1,27 @@
{
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture",
"authors": [
"NobbZ"
],
"contributors": [
"ErikSchierboom",
"iHiD",
"JordanAdams",
"juhlig",
"kytrinyx",
"wojtii"
],
"files": {
"solution": [
"src/collatz_conjecture.erl"
],
"test": [
"test/collatz_conjecture_tests.erl"
],
"example": [
".meta/example.erl"
]
},
"source": "An unsolved problem in mathematics named after mathematician Lothar Collatz",
"source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem"
}

1
erlang/collatz-conjecture/.exercism/metadata.json

@ -0,0 +1 @@
{"track":"erlang","exercise":"collatz-conjecture","id":"6258a251cb6e4d80b70d021f2052b335","url":"https://exercism.org/tracks/erlang/exercises/collatz-conjecture","handle":"JBiason","is_requester":true,"auto_approve":false}

36
erlang/collatz-conjecture/HELP.md

@ -0,0 +1,36 @@
# Help
## Running the tests
You can run the tests by running the following command from the exercise directory.
```bash
$ rebar3 eunit
```
## Submitting your solution
You can submit your solution using the `exercism submit src/collatz_conjecture.erl` 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 [Erlang track's documentation](https://exercism.org/docs/tracks/erlang)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Exercism related BEAM support channel on gitter](https://gitter.im/exercism/xerlang)
- [Erlang Documentation](http://www.erlang.org/doc.html)
- [Learn You Some Erlang for Great Good](http://learnyousomeerlang.com)
- [StackOverflow](http://stackoverflow.com/)

51
erlang/collatz-conjecture/README.md

@ -0,0 +1,51 @@
# Collatz Conjecture
Welcome to Collatz Conjecture on Exercism's Erlang 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
- @NobbZ
### Contributed to by
- @ErikSchierboom
- @iHiD
- @JordanAdams
- @juhlig
- @kytrinyx
- @wojtii
### Based on
An unsolved problem in mathematics named after mathematician Lothar Collatz - https://en.wikipedia.org/wiki/3x_%2B_1_problem

30
erlang/collatz-conjecture/rebar.config

@ -0,0 +1,30 @@
%% Erlang compiler options
{erl_opts, [debug_info, warnings_as_errors]}.
{deps, [{erl_exercism, "0.1.2"}]}.
{dialyzer, [
{warnings, [underspecs, no_return]},
{get_warnings, true},
{plt_apps, top_level_deps}, % top_level_deps | all_deps
{plt_extra_apps, []},
{plt_location, local}, % local | "/my/file/name"
{plt_prefix, "rebar3"},
{base_plt_apps, [stdlib, kernel, crypto]},
{base_plt_location, global}, % global | "/my/file/name"
{base_plt_prefix, "rebar3"}
]}.
%% eunit:test(Tests)
{eunit_tests, []}.
%% Options for eunit:test(Tests, Opts)
{eunit_opts, [verbose]}.
%% == xref ==
{xref_warnings, true}.
%% xref checks to run
{xref_checks, [undefined_function_calls, undefined_functions,
locals_not_used, exports_not_used,
deprecated_function_calls, deprecated_functions]}.

9
erlang/collatz-conjecture/src/collatz_conjecture.app.src

@ -0,0 +1,9 @@
{application, collatz_conjecture,
[{description, "exercism.io - collatz-conjecture"},
{vsn, "0.0.1"},
{modules, []},
{registered, []},
{applications, [kernel,
stdlib]},
{env, []}
]}.

9
erlang/collatz-conjecture/src/collatz_conjecture.erl

@ -0,0 +1,9 @@
-module(collatz_conjecture).
-export([steps/1]).
steps(1) -> 0;
steps(N) when N =< 0 -> error(badarg);
steps(N) when (N rem 2 == 0) -> 1 + steps(N div 2);
steps(N) -> 1 + steps(3*N+1).

36
erlang/collatz-conjecture/test/collatz_conjecture_tests.erl

@ -0,0 +1,36 @@
%% Generated with 'testgen v0.2.0'
%% Revision 2 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/collatz-conjecture/canonical-data.json
%% This file is automatically generated from the exercises canonical data.
-module(collatz_conjecture_tests).
-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").
'1_zero_steps_for_one_test_'() ->
{"zero steps for one",
?_assertMatch(0, collatz_conjecture:steps(1))}.
'2_divide_if_even_test_'() ->
{"divide if even",
?_assertMatch(4, collatz_conjecture:steps(16))}.
'3_even_and_odd_steps_test_'() ->
{"even and odd steps",
?_assertMatch(9, collatz_conjecture:steps(12))}.
'4_large_number_of_even_and_odd_steps_test_'() ->
{"large number of even and odd steps",
?_assertMatch(152, collatz_conjecture:steps(1000000))}.
'5_zero_is_an_error_test_'() ->
{"zero is an error",
?_assertError(badarg, collatz_conjecture:steps(0))}.
'6_negative_value_is_an_error_test_'() ->
{"negative value is an error",
?_assertError(badarg, collatz_conjecture:steps(-15))}.

2
tracks.txt

@ -3,7 +3,7 @@ C I
C# I
Clojure I
Elixir I
Erlang
Erlang I
Haskell
Kotlin
Lua

Loading…
Cancel
Save