Skip to content

Commit 0a4a179

Browse files
square root exercise added (#370)
* square root exercise added * added module interface file * author added
1 parent d9e4ce5 commit 0a4a179

File tree

12 files changed

+6406
-2
lines changed

12 files changed

+6406
-2
lines changed

config.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,11 @@
340340
"practices": [],
341341
"prerequisites": [],
342342
"difficulty": 5,
343+
"status": "deprecated",
343344
"topics": [
344345
"arrays",
345346
"strings"
346-
],
347-
"status": "deprecated"
347+
]
348348
},
349349
{
350350
"slug": "acronym",
@@ -381,6 +381,14 @@
381381
"practices": [],
382382
"prerequisites": [],
383383
"difficulty": 5
384+
},
385+
{
386+
"slug": "square-root",
387+
"name": "Square Root",
388+
"uuid": "a0c26f47-b7ae-4fab-8590-e1f5f32c1b50",
389+
"practices": [],
390+
"prerequisites": [],
391+
"difficulty": 3
384392
}
385393
]
386394
},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Instructions
2+
3+
Your task is to calculate the square root of a given number.
4+
5+
- Try to avoid using the pre-existing math libraries of your language.
6+
- As input you'll be given a positive whole number, i.e. 1, 2, 3, 4…
7+
- You are only required to handle cases where the result is a positive whole number.
8+
9+
Some potential approaches:
10+
11+
- Linear or binary search for a number that gives the input number when squared.
12+
- Successive approximation using Newton's or Heron's method.
13+
- Calculating one digit at a time or one bit at a time.
14+
15+
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.
16+
17+
[integer-square-root]: https://en.wikipedia.org/wiki/Integer_square_root
18+
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Introduction
2+
3+
We are launching a deep space exploration rocket and we need a way to make sure the navigation system stays on target.
4+
5+
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).
6+
7+
The journey will be very long.
8+
To make the batteries last as long as possible, we had to make our rocket's onboard computer very power efficient.
9+
Unfortunately that means that we can't rely on fancy math libraries and functions, as they use more power.
10+
Instead we want to implement our own square root calculation.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": ["therealowenrees"],
3+
"files": {
4+
"solution": [
5+
"src/SquareRoot.re"
6+
],
7+
"test": [
8+
"__tests__/SquareRoot_test.re"
9+
],
10+
"example": [
11+
".meta/src/Example.re"
12+
],
13+
"editor": [
14+
"src/SquareRoot.rei"
15+
]
16+
},
17+
"blurb": "Given a natural radicand, return its square root.",
18+
"source": "wolf99",
19+
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let squareRoot = n => {
2+
let radicand = float_of_int(n);
3+
let rec aux = guess => {
4+
let next = 0.5 *. (guess +. radicand /. guess);
5+
switch (abs_float(next -. guess)) {
6+
| diff when diff < 0.0001 => int_of_float(next)
7+
| _ => aux(next)
8+
};
9+
};
10+
aux(radicand /. 2.0);
11+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[9b748478-7b0a-490c-b87a-609dacf631fd]
13+
description = "root of 1"
14+
15+
[7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb]
16+
description = "root of 4"
17+
18+
[6624aabf-3659-4ae0-a1c8-25ae7f33c6ef]
19+
description = "root of 25"
20+
21+
[93beac69-265e-4429-abb1-94506b431f81]
22+
description = "root of 81"
23+
24+
[fbddfeda-8c4f-4bc4-87ca-6991af35360e]
25+
description = "root of 196"
26+
27+
[c03d0532-8368-4734-a8e0-f96a9eb7fc1d]
28+
description = "root of 65025"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
open Jest;
2+
open Expect;
3+
open SquareRoot;
4+
5+
describe("Square Root", () => {
6+
test("root of 1", () =>
7+
expect(squareRoot(1)) |> toEqual(1)
8+
);
9+
test("root of 4", () =>
10+
expect(squareRoot(4)) |> toEqual(2)
11+
);
12+
test("root of 25", () =>
13+
expect(squareRoot(25)) |> toEqual(5)
14+
);
15+
test("root of 81", () =>
16+
expect(squareRoot(81)) |> toEqual(9)
17+
);
18+
test("root of 196", () =>
19+
expect(squareRoot(196)) |> toEqual(14)
20+
);
21+
test("root of 65025", () =>
22+
expect(squareRoot(65025)) |> toEqual(255)
23+
);
24+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This is the configuration file used by BuckleScript's build system bsb. Its documentation lives here: http://bucklescript.github.io/bucklescript/docson/#build-schema.json
2+
// BuckleScript comes with its own parser for bsconfig.json, which is normal JSON, with the extra support of comments and trailing commas.
3+
{
4+
"name": "square-root",
5+
"version": "0.1.0",
6+
"sources": [
7+
{
8+
"dir" : "src",
9+
"subdirs" : true
10+
},
11+
{
12+
"dir": "__tests__",
13+
"type": "dev"
14+
}
15+
],
16+
"package-specs": {
17+
"module": "commonjs",
18+
"in-source": true
19+
},
20+
"suffix": ".bs.js",
21+
"bs-dependencies": [
22+
// add your dependencies here. You'd usually install them normally through `npm install my-dependency`. If my-dependency has a bsconfig.json too, then everything will work seamlessly.
23+
],
24+
"bs-dev-dependencies": ["@glennsl/bs-jest"],
25+
"warnings": {
26+
"error" : "+101"
27+
},
28+
"namespace": true,
29+
"refmt": 3
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "exercism-reasonml",
3+
"version": "0.1.0",
4+
"scripts": {
5+
"build": "bsb -make-world",
6+
"start": "bsb -make-world -w",
7+
"clean": "bsb -clean-world",
8+
"test": "jest --watchAll --env=node",
9+
"test:ci": "jest --ci --bail --no-cache --env=node"
10+
},
11+
"keywords": [
12+
"BuckleScript"
13+
],
14+
"author": "",
15+
"license": "MIT",
16+
"devDependencies": {
17+
"@glennsl/bs-jest": "^0.7.0",
18+
"bs-platform": "^9.0.2"
19+
}
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let squareRoot = (_) => failwith = "squareRoot function not implemented"

0 commit comments

Comments
 (0)