Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Exercise]: Square Root #246

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,16 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "84ab45f7-243e-43ee-bd56-4ac6267f4206",
"practices": [
"math"
],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "high-scores",
"name": "High Scores",
Expand Down
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
38 changes: 38 additions & 0 deletions exercises/practice/square-root/.meta/SquareRoot.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Function Get-SquareRoot() {
<#
.SYNOPSIS
Given a natural radicand, return its square root.

.DESCRIPTION
The function takes a positive integer and return its square root value.

.PARAMETER Radicand
The number to get its square root.

.EXAMPLE
Get-SquareRoot -Radicand 25
Retuns: 5
#>
[CmdletBinding()]
Param(
[int]$Radicand
)
$Guess = $Radicand
$Divisor = 0
$Term = 1 -shl 30

while ($Term -gt $Radicand) {
$Term = $Term -shr 2
}

while ($Term) {
if ($Guess -ge ($Divisor + $Term)) {
$Guess -= ($Divisor + $Term)
$Divisor = ($Divisor -shr 1) + $Term
}else {
$Divisor = $Divisor -shr 1
}
$Term = $Term -shr 2
}
return $Divisor
}
17 changes: 17 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["glaxxie"],
"files": {
"solution": [
"SquareRoot.ps1"
],
"test": [
"SquareRoot.tests.ps1"
],
"example": [
".meta/SquareRoot.example.ps1"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
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"
21 changes: 21 additions & 0 deletions exercises/practice/square-root/SquareRoot.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Function Get-SquareRoot() {
<#
.SYNOPSIS
Given a natural radicand, return its square root.

.DESCRIPTION
The function takes a positive integer and return its square root value.

.PARAMETER Radicand
The number to get its square root.

.EXAMPLE
Get-SquareRoot -Radicand 25
Retuns: 5
#>
[CmdletBinding()]
Param(
[int]$Radicand
)
Throw "Please implement this function"
}
29 changes: 29 additions & 0 deletions exercises/practice/square-root/SquareRoot.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BeforeAll {
. "./SquareRoot.ps1"
}

Describe "Square root test cases" {
It "root of 1" {
Get-SquareRoot -Radicand 1 | Should -Be 1
}

It "root of 4" {
Get-SquareRoot -Radicand 4 | Should -Be 2
}

It "root of 25" {
Get-SquareRoot -Radicand 25 | Should -Be 5
}

It "root of 81" {
Get-SquareRoot -Radicand 81 | Should -Be 9
}

It "root of 196" {
Get-SquareRoot -Radicand 196 | Should -Be 14
}

It "root of 65025" {
Get-SquareRoot -Radicand 65025 | Should -Be 255
}
}