From 4408bcd32a10373892a123be02415b7ff69d13b5 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 5 Oct 2021 19:12:04 -0300 Subject: [PATCH] Exercism: 3x+1 --- .../collatz-conjecture/.exercism/config.json | 27 ++++++++++ .../.exercism/metadata.json | 1 + erlang/collatz-conjecture/HELP.md | 36 +++++++++++++ erlang/collatz-conjecture/README.md | 51 +++++++++++++++++++ erlang/collatz-conjecture/rebar.config | 30 +++++++++++ .../src/collatz_conjecture.app.src | 9 ++++ .../src/collatz_conjecture.erl | 9 ++++ .../test/collatz_conjecture_tests.erl | 36 +++++++++++++ tracks.txt | 2 +- 9 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 erlang/collatz-conjecture/.exercism/config.json create mode 100644 erlang/collatz-conjecture/.exercism/metadata.json create mode 100644 erlang/collatz-conjecture/HELP.md create mode 100644 erlang/collatz-conjecture/README.md create mode 100644 erlang/collatz-conjecture/rebar.config create mode 100644 erlang/collatz-conjecture/src/collatz_conjecture.app.src create mode 100644 erlang/collatz-conjecture/src/collatz_conjecture.erl create mode 100644 erlang/collatz-conjecture/test/collatz_conjecture_tests.erl diff --git a/erlang/collatz-conjecture/.exercism/config.json b/erlang/collatz-conjecture/.exercism/config.json new file mode 100644 index 0000000..cb446e2 --- /dev/null +++ b/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" +} diff --git a/erlang/collatz-conjecture/.exercism/metadata.json b/erlang/collatz-conjecture/.exercism/metadata.json new file mode 100644 index 0000000..eb30814 --- /dev/null +++ b/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} \ No newline at end of file diff --git a/erlang/collatz-conjecture/HELP.md b/erlang/collatz-conjecture/HELP.md new file mode 100644 index 0000000..9f00042 --- /dev/null +++ b/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/) \ No newline at end of file diff --git a/erlang/collatz-conjecture/README.md b/erlang/collatz-conjecture/README.md new file mode 100644 index 0000000..b303969 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/erlang/collatz-conjecture/rebar.config b/erlang/collatz-conjecture/rebar.config new file mode 100644 index 0000000..db5d907 --- /dev/null +++ b/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]}. diff --git a/erlang/collatz-conjecture/src/collatz_conjecture.app.src b/erlang/collatz-conjecture/src/collatz_conjecture.app.src new file mode 100644 index 0000000..4f28a0c --- /dev/null +++ b/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, []} + ]}. diff --git a/erlang/collatz-conjecture/src/collatz_conjecture.erl b/erlang/collatz-conjecture/src/collatz_conjecture.erl new file mode 100644 index 0000000..d87aa6b --- /dev/null +++ b/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). diff --git a/erlang/collatz-conjecture/test/collatz_conjecture_tests.erl b/erlang/collatz-conjecture/test/collatz_conjecture_tests.erl new file mode 100644 index 0000000..653bb66 --- /dev/null +++ b/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))}. diff --git a/tracks.txt b/tracks.txt index 43b675b..7bd9729 100644 --- a/tracks.txt +++ b/tracks.txt @@ -3,7 +3,7 @@ C I C# I Clojure I Elixir I -Erlang +Erlang I Haskell Kotlin Lua