diff --git a/config.json b/config.json index 70ba1bc..a5f366b 100644 --- a/config.json +++ b/config.json @@ -318,6 +318,14 @@ "practices": [], "prerequisites": [], "difficulty": 3 + }, + { + "slug": "square-root", + "name": "Square Root", + "uuid": "44f3a1bb-b92b-4f97-ac36-52927df97b3c", + "practices": [], + "prerequisites": [], + "difficulty": 3 } ] }, diff --git a/exercises/practice/square-root/.busted b/exercises/practice/square-root/.busted new file mode 100644 index 0000000..86b84e7 --- /dev/null +++ b/exercises/practice/square-root/.busted @@ -0,0 +1,5 @@ +return { + default = { + ROOT = { '.' } + } +} diff --git a/exercises/practice/square-root/.docs/instructions.md b/exercises/practice/square-root/.docs/instructions.md new file mode 100644 index 0000000..d258b86 --- /dev/null +++ b/exercises/practice/square-root/.docs/instructions.md @@ -0,0 +1,18 @@ +# Instructions + +Your task is to calculate the square root of a given number. + +- Try to avoid using the pre-existing math libraries of your language. +- As input you'll be given a positive whole number, i.e. 1, 2, 3, 4… +- You are only required to handle cases where the result is a positive whole number. + +Some potential approaches: + +- Linear or binary search for a number that gives the input number when squared. +- Successive approximation using Newton's or Heron's method. +- Calculating one digit at a time or one bit at a time. + +You can check out the Wikipedia pages on [integer square root][integer-square-root] and [methods of computing square roots][computing-square-roots] to help with choosing a method of calculation. + +[integer-square-root]: https://en.wikipedia.org/wiki/Integer_square_root +[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots diff --git a/exercises/practice/square-root/.docs/introduction.md b/exercises/practice/square-root/.docs/introduction.md new file mode 100644 index 0000000..1d69293 --- /dev/null +++ b/exercises/practice/square-root/.docs/introduction.md @@ -0,0 +1,10 @@ +# Introduction + +We are launching a deep space exploration rocket and we need a way to make sure the navigation system stays on target. + +As the first step in our calculation, we take a target number and find its square root (that is, the number that when multiplied by itself equals the target number). + +The journey will be very long. +To make the batteries last as long as possible, we had to make our rocket's onboard computer very power efficient. +Unfortunately that means that we can't rely on fancy math libraries and functions, as they use more power. +Instead we want to implement our own square root calculation. diff --git a/exercises/practice/square-root/.meta/config.json b/exercises/practice/square-root/.meta/config.json new file mode 100644 index 0000000..67433c0 --- /dev/null +++ b/exercises/practice/square-root/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "square_root.moon" + ], + "test": [ + "square_root_spec.moon" + ], + "example": [ + ".meta/example.moon" + ] + }, + "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.moon b/exercises/practice/square-root/.meta/example.moon new file mode 100644 index 0000000..6766041 --- /dev/null +++ b/exercises/practice/square-root/.meta/example.moon @@ -0,0 +1,17 @@ +-- Using the Binary numeral system (base 2) from Wikipedia +-- https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_%28base_2%29 + +sqrt = (n) -> + b = 4 ^ (math.floor math.log(n, 4)) + x = 0 + + while b != 0 + if n >= x + b + n -= x + b + x = (x >> 1) + b + else + x >>= 1 + b >>= 2 + x + +{ :sqrt } diff --git a/exercises/practice/square-root/.meta/spec_generator.moon b/exercises/practice/square-root/.meta/spec_generator.moon new file mode 100644 index 0000000..cf3a0a3 --- /dev/null +++ b/exercises/practice/square-root/.meta/spec_generator.moon @@ -0,0 +1,9 @@ +{ + module_name: 'SquareRoot', + generate_test: (case, level) -> + lines = { + "result = SquareRoot.sqrt #{case.input.radicand}", + "assert.are.equal #{case.expected}, result" + } + table.concat [indent line, level for line in *lines], '\n' +} diff --git a/exercises/practice/square-root/.meta/tests.toml b/exercises/practice/square-root/.meta/tests.toml new file mode 100644 index 0000000..ead7882 --- /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/square_root.moon b/exercises/practice/square-root/square_root.moon new file mode 100644 index 0000000..43f7a78 --- /dev/null +++ b/exercises/practice/square-root/square_root.moon @@ -0,0 +1,4 @@ +square_root = (radicand) -> + error 'Implement me' + +{ sqrt: square_root } diff --git a/exercises/practice/square-root/square_root_spec.moon b/exercises/practice/square-root/square_root_spec.moon new file mode 100644 index 0000000..f99a5a5 --- /dev/null +++ b/exercises/practice/square-root/square_root_spec.moon @@ -0,0 +1,26 @@ +SquareRoot = require 'square_root' + +describe 'square-root', -> + it 'root of 1', -> + result = SquareRoot.sqrt 1 + assert.are.equal 1, result + + pending 'root of 4', -> + result = SquareRoot.sqrt 4 + assert.are.equal 2, result + + pending 'root of 25', -> + result = SquareRoot.sqrt 25 + assert.are.equal 5, result + + pending 'root of 81', -> + result = SquareRoot.sqrt 81 + assert.are.equal 9, result + + pending 'root of 196', -> + result = SquareRoot.sqrt 196 + assert.are.equal 14, result + + pending 'root of 65025', -> + result = SquareRoot.sqrt 65025 + assert.are.equal 255, result