Skip to content

Commit

Permalink
Add square-root (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Nov 15, 2023
1 parent 04ee923 commit babf0a1
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@
"hash_tables"
]
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "35e7b8b4-540e-4388-9282-9a6872cb939c",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "binary",
"name": "Binary",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/square-root/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
13 changes: 13 additions & 0 deletions exercises/practice/square-root/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"square-root.lua"
],
"test": [
"square-root_spec.lua"
],
"example": [
".meta/example.lua"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
12 changes: 12 additions & 0 deletions exercises/practice/square-root/.meta/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local SquareRoot = {}

function SquareRoot.squareRoot(radicand)
local root = 0
while root ^ 2 ~= radicand do
root = root + 1
end

return root
end

return SquareRoot
28 changes: 28 additions & 0 deletions exercises/practice/square-root/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions exercises/practice/square-root/square-root.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local SquareRoot = {}

function SquareRoot.squareRoot(radicand)

end

return SquareRoot
27 changes: 27 additions & 0 deletions exercises/practice/square-root/square-root_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local SquareRoot = require 'square-root'

describe('square-root', function()
it('root of 1', function()
assert.equal(1, SquareRoot.squareRoot(1))
end)

it('root of 4', function()
assert.equal(2, SquareRoot.squareRoot(4))
end)

it('root of 25', function()
assert.equal(5, SquareRoot.squareRoot(25))
end)

it('root of 81', function()
assert.equal(9, SquareRoot.squareRoot(81))
end)

it('root of 196', function()
assert.equal(14, SquareRoot.squareRoot(196))
end)

it('root of 65025', function()
assert.equal(255, SquareRoot.squareRoot(65025))
end)
end)

0 comments on commit babf0a1

Please sign in to comment.