Skip to content

Commit 841dc7b

Browse files
Implement practice exercise square-root (exercism#2362)
* Add empty exercise structure * Add tests and stub implementation * Add reference implementation * Steer students away from `Math.sqrt` * Increase difficulty * Revert changes to exercises/build.gradle This is being fixed in exercism#2363 instead.
1 parent bcae59e commit 841dc7b

11 files changed

Lines changed: 182 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,14 @@
19231923
"practices": ["lists", "classes", "for-loops"],
19241924
"prerequisites" :["lists"],
19251925
"difficulty": 2
1926+
},
1927+
{
1928+
"slug": "square-root",
1929+
"name": "Square Root",
1930+
"uuid": "61886554-ec84-422a-bbf9-aeee37c45bb6",
1931+
"practices": ["numbers"],
1932+
"prerequisites": ["numbers"],
1933+
"difficulty": 5
19261934
}
19271935
],
19281936
"foregone": []
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Instructions (appended)
2+
3+
The goal of this exercise is to practice working with numbers, so stay away
4+
from `java.lang.Math`!
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Instructions
2+
3+
Given a natural radicand, return its square root.
4+
5+
Note that the term "radicand" refers to the number for which the root is to be determined.
6+
That is, it is the number under the root symbol.
7+
8+
Check out the Wikipedia pages on [square root][square-root] and [methods of computing square roots][computing-square-roots].
9+
10+
Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up).
11+
12+
[square-root]: https://en.wikipedia.org/wiki/Square_root
13+
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"authors": ["sanderploegsma"],
3+
"contributors": [],
4+
"files": {
5+
"solution": ["src/main/java/SquareRoot.java"],
6+
"test": ["src/test/java/SquareRootTest.java"],
7+
"example": [".meta/src/reference/java/SquareRoot.java"],
8+
"invalidator": ["build.gradle"]
9+
},
10+
"blurb": "Given a natural radicand, return its square root.",
11+
"source": "wolf99",
12+
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class SquareRoot {
2+
/**
3+
* Finds the square root of the given radicand using digit-by-digit calculation.
4+
*
5+
* @param radicand a natural number
6+
* @return square root of the given radicand
7+
* @see <a href="https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2)">
8+
* Source on Wikipedia
9+
* </a>
10+
*/
11+
public int squareRoot(int radicand) {
12+
int x = radicand;
13+
int c = 0;
14+
int d = 1 << 30;
15+
16+
while (d > radicand) {
17+
d >>= 2;
18+
}
19+
20+
while (d != 0) {
21+
if (x >= c + d) {
22+
x -= c + d;
23+
c = (c >> 1) + d;
24+
} else {
25+
c >>= 1;
26+
}
27+
d >>= 2;
28+
}
29+
30+
return c;
31+
}
32+
}
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
id "java"
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation platform("org.junit:junit-bom:5.10.0")
11+
testImplementation "org.junit.jupiter:junit-jupiter"
12+
testImplementation "org.assertj:assertj-core:3.15.0"
13+
}
14+
15+
test {
16+
useJUnitPlatform()
17+
18+
testLogging {
19+
exceptionFormat = "full"
20+
showStandardStreams = true
21+
events = ["passed", "failed", "skipped"]
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class SquareRoot {
2+
public int squareRoot(int radicand) {
3+
throw new UnsupportedOperationException("Please implement the SquareRoot.squareRoot method.");
4+
}
5+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import org.junit.jupiter.api.Disabled;
2+
import org.junit.jupiter.api.DisplayName;
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class SquareRootTest {
8+
@Test
9+
@DisplayName("Square root of 1")
10+
public void squareRootOf1() {
11+
assertThat(new SquareRoot().squareRoot(1))
12+
.isEqualTo(1);
13+
}
14+
15+
@Disabled("Remove to run test")
16+
@Test
17+
@DisplayName("Square root of 4")
18+
public void squareRootOf4() {
19+
assertThat(new SquareRoot().squareRoot(4))
20+
.isEqualTo(2);
21+
}
22+
23+
@Disabled("Remove to run test")
24+
@Test
25+
@DisplayName("Square root of 25")
26+
public void squareRootOf25() {
27+
assertThat(new SquareRoot().squareRoot(25))
28+
.isEqualTo(5);
29+
}
30+
31+
@Disabled("Remove to run test")
32+
@Test
33+
@DisplayName("Square root of 81")
34+
public void squareRootOf81() {
35+
assertThat(new SquareRoot().squareRoot(81))
36+
.isEqualTo(9);
37+
}
38+
39+
@Disabled("Remove to run test")
40+
@Test
41+
@DisplayName("Square root of 196")
42+
public void squareRootOf196() {
43+
assertThat(new SquareRoot().squareRoot(196))
44+
.isEqualTo(14);
45+
}
46+
47+
@Disabled("Remove to run test")
48+
@Test
49+
@DisplayName("Square root of 65025")
50+
public void squareRootOf65025() {
51+
assertThat(new SquareRoot().squareRoot(65025))
52+
.isEqualTo(255);
53+
}
54+
}

0 commit comments

Comments
 (0)