Skip to content

Commit 8429580

Browse files
Square root exercise (#548)
1 parent 20c62f0 commit 8429580

File tree

14 files changed

+166
-0
lines changed

14 files changed

+166
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,14 @@
598598
"practices": [],
599599
"prerequisites": [],
600600
"difficulty": 3
601+
},
602+
{
603+
"slug": "square-root",
604+
"name": "Square Root",
605+
"uuid": "33360ed8-c49c-4c7f-a1fb-7e242b0794c0",
606+
"practices": [],
607+
"prerequisites": [],
608+
"difficulty": 3
601609
}
602610
]
603611
},
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+
"square_root.ml"
6+
],
7+
"test": [
8+
"test.ml"
9+
],
10+
"example": [
11+
".meta/example.ml"
12+
],
13+
"editor": [
14+
"square_root.mli"
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let square_root n =
2+
let radicand = float_of_int n in
3+
let rec aux guess =
4+
let next = 0.5 *. (guess +. radicand /. guess) in
5+
match abs_float (next -. guess) with
6+
| diff when diff < 0.0001 -> int_of_float next
7+
| _ -> aux next
8+
in
9+
if n < 0 then invalid_arg "square_root: negative input"
10+
else aux (radicand /. 2.0)
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
default: clean test
2+
3+
test:
4+
dune runtest
5+
6+
clean:
7+
dune clean
8+
9+
.PHONY: clean

exercises/practice/square-root/dune

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(executable
2+
(name test)
3+
(libraries base ounit2))
4+
5+
(alias
6+
(name runtest)
7+
(deps
8+
(:x test.exe))
9+
(action
10+
(run %{x})))
11+
12+
(alias
13+
(name buildtest)
14+
(deps
15+
(:x test.exe)))
16+
17+
(env
18+
(dev
19+
(flags
20+
(:standard -warn-error -A))))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.1)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let square_root _ =
2+
failwith = "square root not implemented"

0 commit comments

Comments
 (0)