diff --git a/config.json b/config.json index bcc18f0d..3b5286e1 100644 --- a/config.json +++ b/config.json @@ -143,6 +143,14 @@ "strings" ] }, + { + "slug": "square-root", + "name": "Square Root", + "uuid": "d320289e-3e3c-4ca4-a5a4-76072cb2bc64", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "strain", "name": "Strain", diff --git a/exercises/practice/square-root/.docs/instructions.md b/exercises/practice/square-root/.docs/instructions.md new file mode 100644 index 00000000..e9905e9d --- /dev/null +++ b/exercises/practice/square-root/.docs/instructions.md @@ -0,0 +1,13 @@ +# Instructions + +Given a natural radicand, return its square root. + +Note that the term "radicand" refers to the number for which the root is to be determined. +That is, it is the number under the root symbol. + +Check out the Wikipedia pages on [square root][square-root] and [methods of computing square roots][computing-square-roots]. + +Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up). + +[square-root]: https://en.wikipedia.org/wiki/Square_root +[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots diff --git a/exercises/practice/square-root/.meta/config.json b/exercises/practice/square-root/.meta/config.json new file mode 100644 index 00000000..b6c4498b --- /dev/null +++ b/exercises/practice/square-root/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/square_root.erl" + ], + "test": [ + "test/square_root_tests.erl" + ], + "example": [ + ".meta/example.erl" + ] + }, + "blurb": "Given a natural radicand, return its square root.", + "source": "wolf99", + "source_url": "https://github.com/exercism/problem-specifications/pull/1582" +} diff --git a/exercises/practice/square-root/.meta/example.erl b/exercises/practice/square-root/.meta/example.erl new file mode 100644 index 00000000..1ec5267b --- /dev/null +++ b/exercises/practice/square-root/.meta/example.erl @@ -0,0 +1,13 @@ +-module(example). + +-export([square_root/1]). + + +square_root(1) -> + 1; +square_root(Radicand) -> + square_root(Radicand, Radicand div 2). +square_root(Radicand, Guess) when (Guess * Guess) =:= Radicand -> + Guess; +square_root(Radicand, Guess) -> + square_root(Radicand, (Guess + Radicand div Guess) div 2). diff --git a/exercises/practice/square-root/.meta/tests.toml b/exercises/practice/square-root/.meta/tests.toml new file mode 100644 index 00000000..ead7882f --- /dev/null +++ b/exercises/practice/square-root/.meta/tests.toml @@ -0,0 +1,28 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[9b748478-7b0a-490c-b87a-609dacf631fd] +description = "root of 1" + +[7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb] +description = "root of 4" + +[6624aabf-3659-4ae0-a1c8-25ae7f33c6ef] +description = "root of 25" + +[93beac69-265e-4429-abb1-94506b431f81] +description = "root of 81" + +[fbddfeda-8c4f-4bc4-87ca-6991af35360e] +description = "root of 196" + +[c03d0532-8368-4734-a8e0-f96a9eb7fc1d] +description = "root of 65025" diff --git a/exercises/practice/square-root/rebar.config b/exercises/practice/square-root/rebar.config new file mode 100644 index 00000000..db5d9076 --- /dev/null +++ b/exercises/practice/square-root/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/exercises/practice/square-root/src/square_root.app.src b/exercises/practice/square-root/src/square_root.app.src new file mode 100644 index 00000000..8abdd11d --- /dev/null +++ b/exercises/practice/square-root/src/square_root.app.src @@ -0,0 +1,9 @@ +{application, square_root, + [{description, "exercism.org - square root"}, + {vsn, "0.0.1"}, + {modules, []}, + {registered, []}, + {applications, [kernel, + stdlib]}, + {env, []} + ]}. diff --git a/exercises/practice/square-root/src/square_root.erl b/exercises/practice/square-root/src/square_root.erl new file mode 100644 index 00000000..c7960e6b --- /dev/null +++ b/exercises/practice/square-root/src/square_root.erl @@ -0,0 +1,6 @@ +-module(square_root). + +-export([square_root/1]). + + +square(_Radicand) -> undefined. diff --git a/exercises/practice/square-root/test/square_root_tests.erl b/exercises/practice/square-root/test/square_root_tests.erl new file mode 100644 index 00000000..fa9a9e5b --- /dev/null +++ b/exercises/practice/square-root/test/square_root_tests.erl @@ -0,0 +1,31 @@ +-module(square_root_tests). + +-include_lib("erl_exercism/include/exercism.hrl"). +-include_lib("eunit/include/eunit.hrl"). + + + + +'1_root_of_1_test_'() -> + {"root of l", + ?_assertMatch(1, square_root:square_root(1))}. + +'2_root_of_4_test_'() -> + {"root of 4", + ?_assertMatch(2, square_root:square_root(4))}. + +'3_root_of_25_test_'() -> + {"root of 25", + ?_assertMatch(5, square_root:square_root(25))}. + +'4_root_of_81_test_'() -> + {"root of 81", + ?_assertMatch(9, square_root:square_root(81))}. + +'5_root_of_196_test_'() -> + {"root of 196", + ?_assertMatch(14, square_root:square_root(196))}. + +'6_root_of_65025_test_'() -> + {"root of 65025", + ?_assertMatch(255, square_root:square_root(65025))}.