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

[square-root] Implementation #634

Merged
merged 3 commits into from Jul 18, 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
9 changes: 9 additions & 0 deletions config.json
Expand Up @@ -598,6 +598,15 @@
"prerequisites": [],
"difficulty": 1,
"topics": []
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "165e9f04-5c0e-458d-b34f-3535ae6296fd",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
}
]
},
Expand Down
13 changes: 13 additions & 0 deletions 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
19 changes: 19 additions & 0 deletions exercises/practice/square-root/.meta/config.json
@@ -0,0 +1,19 @@
{
"authors": [
"habere-et-dispertire"
],
"files": {
"solution": [
"SquareRoot.rakumod"
],
"test": [
"square-root.rakutest"
],
"example": [
".meta/solutions/SquareRoot.rakumod"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
14 changes: 14 additions & 0 deletions exercises/practice/square-root/.meta/solutions/SquareRoot.rakumod
@@ -0,0 +1,14 @@
unit module SquareRoot;

proto square-root (|) is export {*}
multi square-root( 0 ) { 0 }
multi square-root( 1 ) { 1 }
multi square-root( $n ) {
my $x0 = 2 ** ( floor( log2 $n / 2 ) + 1 );
my $x1 = ( $x0 + $n div $x0 ) div 2;
while $x1 < $x0 {
$x0 = $x1;
$x1 = ( $x0 + $n div $x0 ) div 2;
}
return $x0;
}
38 changes: 38 additions & 0 deletions exercises/practice/square-root/.meta/template-data.yaml
@@ -0,0 +1,38 @@
unit: module

tests: |
for Num, Complex, FatRat, Rat, Int {
.^find_method(<sqrt>).wrap: sub (|) {
bail-out 'Calling the sqrt method is not allowed for this exercise.'
}
}

properties:
squareRoot:
test: |-
sprintf(q:to/END/, (%case<input><radicand>, %case<expected>, %case<description>).map(*.raku));
cmp-ok(
square-root(%s),
"==",
%s,
%s,
);
END

example: |-
proto square-root (|) is export {*}
multi square-root( 0 ) { 0 }
multi square-root( 1 ) { 1 }
multi square-root( $n ) {
my $x0 = 2 ** ( floor( log2 $n / 2 ) + 1 );
my $x1 = ( $x0 + $n div $x0 ) div 2;
while $x1 < $x0 {
$x0 = $x1;
$x1 = ( $x0 + $n div $x0 ) div 2;
}
return $x0;
}

stub: |-
sub square-root( $n ) is export {
}
28 changes: 28 additions & 0 deletions 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"
4 changes: 4 additions & 0 deletions exercises/practice/square-root/SquareRoot.rakumod
@@ -0,0 +1,4 @@
unit module SquareRoot;

sub square-root( $n ) is export {
}
54 changes: 54 additions & 0 deletions exercises/practice/square-root/square-root.rakutest
@@ -0,0 +1,54 @@
#!/usr/bin/env raku
use Test;
use lib $?FILE.IO.dirname;
use SquareRoot;

for Num, Complex, FatRat, Rat, Int {
.^find_method(<sqrt>).wrap: sub (|) {
bail-out 'Calling the sqrt method is not allowed for this exercise.'
}
}

cmp-ok( # begin: 9b748478-7b0a-490c-b87a-609dacf631fd
square-root(1),
"==",
1,
"root of 1",
); # end: 9b748478-7b0a-490c-b87a-609dacf631fd

cmp-ok( # begin: 7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb
square-root(4),
"==",
2,
"root of 4",
); # end: 7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb

cmp-ok( # begin: 6624aabf-3659-4ae0-a1c8-25ae7f33c6ef
square-root(25),
"==",
5,
"root of 25",
); # end: 6624aabf-3659-4ae0-a1c8-25ae7f33c6ef

cmp-ok( # begin: 93beac69-265e-4429-abb1-94506b431f81
square-root(81),
"==",
9,
"root of 81",
); # end: 93beac69-265e-4429-abb1-94506b431f81

cmp-ok( # begin: fbddfeda-8c4f-4bc4-87ca-6991af35360e
square-root(196),
"==",
14,
"root of 196",
); # end: fbddfeda-8c4f-4bc4-87ca-6991af35360e

cmp-ok( # begin: c03d0532-8368-4734-a8e0-f96a9eb7fc1d
square-root(65025),
"==",
255,
"root of 65025",
); # end: c03d0532-8368-4734-a8e0-f96a9eb7fc1d

done-testing;