diff --git a/config.json b/config.json index 9f38cff808..96d746c9e7 100644 --- a/config.json +++ b/config.json @@ -2611,6 +2611,23 @@ ], "difficulty": 8 }, + { + "slug": "square-root", + "name": "Square Root", + "uuid": "bf9e6251-105b-4fd7-b388-c6f5a652d174", + "prerequisites": [ + "if", + "cond", + "integer", + "multiple-clause-functions", + "pattern-matching", + "recursion" + ], + "practices": [ + "integer" + ], + "difficulty": 5 + }, { "slug": "sgf-parsing", "name": "SGF Parsing", diff --git a/exercises/practice/square-root/.docs/instructions.append.md b/exercises/practice/square-root/.docs/instructions.append.md new file mode 100644 index 0000000000..b95755825a --- /dev/null +++ b/exercises/practice/square-root/.docs/instructions.append.md @@ -0,0 +1,3 @@ +# Introduction append + +Make sure that you implement an algorithm that doesn't rely on built-in function such as `:math.sqrt()` or `Float.pow`. diff --git a/exercises/practice/square-root/.docs/instructions.md b/exercises/practice/square-root/.docs/instructions.md new file mode 100644 index 0000000000..6e430023af --- /dev/null +++ b/exercises/practice/square-root/.docs/instructions.md @@ -0,0 +1,9 @@ +# Description + +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](https://en.wikipedia.org/wiki/Square_root) and [methods of computing square roots](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots). + +Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up). diff --git a/exercises/practice/square-root/.formatter.exs b/exercises/practice/square-root/.formatter.exs new file mode 100644 index 0000000000..d2cda26edd --- /dev/null +++ b/exercises/practice/square-root/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/exercises/practice/square-root/.meta/config.json b/exercises/practice/square-root/.meta/config.json new file mode 100644 index 0000000000..55289dd546 --- /dev/null +++ b/exercises/practice/square-root/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": ["jiegillet"], + "contributors": [], + "files": { + "example": [ + ".meta/example.ex" + ], + "solution": [ + "lib/square_root.ex" + ], + "test": [ + "test/square_root_test.exs" + ] + }, + "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.ex b/exercises/practice/square-root/.meta/example.ex new file mode 100644 index 0000000000..ca2008e0fb --- /dev/null +++ b/exercises/practice/square-root/.meta/example.ex @@ -0,0 +1,22 @@ +defmodule SquareRoot do + @doc """ + Calculate the integer square root of a positive integer + """ + @spec calculate(radicand :: pos_integer) :: pos_integer + def calculate(1), do: 1 + + def calculate(radicand) do + guess = div(radicand, 2) + calculate(radicand, guess) + end + + def calculate(radicand, guess) do + new_guess = div(guess + div(radicand, guess), 2) + + if new_guess == guess do + guess + else + calculate(radicand, new_guess) + end + end +end diff --git a/exercises/practice/square-root/lib/square_root.ex b/exercises/practice/square-root/lib/square_root.ex new file mode 100644 index 0000000000..9ef8224be3 --- /dev/null +++ b/exercises/practice/square-root/lib/square_root.ex @@ -0,0 +1,8 @@ +defmodule SquareRoot do + @doc """ + Calculate the integer square root of a positive integer + """ + @spec calculate(radicand :: pos_integer) :: pos_integer + def calculate(radicand) do + end +end diff --git a/exercises/practice/square-root/mix.exs b/exercises/practice/square-root/mix.exs new file mode 100644 index 0000000000..1feae9277c --- /dev/null +++ b/exercises/practice/square-root/mix.exs @@ -0,0 +1,28 @@ +defmodule SquareRoot.MixProject do + use Mix.Project + + def project do + [ + app: :square_root, + 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 diff --git a/exercises/practice/square-root/test/square_root_test.exs b/exercises/practice/square-root/test/square_root_test.exs new file mode 100644 index 0000000000..e2af7840de --- /dev/null +++ b/exercises/practice/square-root/test/square_root_test.exs @@ -0,0 +1,57 @@ +defmodule SquareRootTest do + use ExUnit.Case + + # @tag :pending + test "root of 1" do + radicand = 1 + output = SquareRoot.calculate(radicand) + expected = 1 + + assert output == expected + end + + @tag :pending + test "root of 4" do + radicand = 4 + output = SquareRoot.calculate(radicand) + expected = 2 + + assert output == expected + end + + @tag :pending + test "root of 25" do + radicand = 25 + output = SquareRoot.calculate(radicand) + expected = 5 + + assert output == expected + end + + @tag :pending + test "root of 81" do + radicand = 81 + output = SquareRoot.calculate(radicand) + expected = 9 + + assert output == expected + end + + @tag :pending + test "root of 196" do + radicand = 196 + output = SquareRoot.calculate(radicand) + expected = 14 + + assert output == expected + end + + @tag :pending + test "root of 65025" do + radicand = 65025 + output = SquareRoot.calculate(radicand) + expected = 255 + + assert output == expected + end +end diff --git a/exercises/practice/square-root/test/test_helper.exs b/exercises/practice/square-root/test/test_helper.exs new file mode 100644 index 0000000000..35fc5bff82 --- /dev/null +++ b/exercises/practice/square-root/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)