Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,18 @@
"randomness"
]
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "e277811f-3dbc-48c5-ae96-8c14c68564e2",
"practices": [],
"prerequisites": [],
"difficulty": 2,
"topics": [
"math",
"integers"
]
},
{
"slug": "binary-search",
"name": "Binary Search",
Expand Down
18 changes: 18 additions & 0 deletions exercises/practice/square-root/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Instructions

Your task is to calculate the square root of a given number.

- Try to avoid using the pre-existing math libraries of your language.
- As input you'll be given a positive whole number, i.e. 1, 2, 3, 4…
- You are only required to handle cases where the result is a positive whole number.

Some potential approaches:

- Linear or binary search for a number that gives the input number when squared.
- Successive approximation using Newton's or Heron's method.
- Calculating one digit at a time or one bit at a time.

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.

[integer-square-root]: https://en.wikipedia.org/wiki/Integer_square_root
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
10 changes: 10 additions & 0 deletions exercises/practice/square-root/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Introduction

We are launching a deep space exploration rocket and we need a way to make sure the navigation system stays on target.

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).

The journey will be very long.
To make the batteries last as long as possible, we had to make our rocket's onboard computer very power efficient.
Unfortunately that means that we can't rely on fancy math libraries and functions, as they use more power.
Instead we want to implement our own square root calculation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
enum SquareRootError: Error {
case negativeInput
}

struct SquareRoot {
static func squareRoot(_ n: Int) throws -> Int {
guard n >= 0 else {
throw SquareRootError.negativeInput
}
if n == 0 || n == 1 {
return n
}
var low = 1
var high = n
var result = 1
while low <= high {
let mid = low + (high - low) / 2
if mid <= n / mid {
result = mid
low = mid + 1
} else {
high = mid - 1
}
}
return result
}
}
19 changes: 19 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"Sencudra"
],
"files": {
"solution": [
"Sources/SquareRoot/SquareRoot.swift"
],
"test": [
"Tests/SquareRootTests/SquareRootTests.swift"
],
"example": [
".meta/Sources/SquareRoot/SquareRootExample.swift"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
22 changes: 22 additions & 0 deletions exercises/practice/square-root/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Testing
import Foundation

@testable import {{ exercise|camelCase }}

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct {{ exercise|camelCase }}Tests {
{% for case in cases %}
{% if forloop.first -%}
@Test("{{ case.description }}")
{% else -%}
@Test("{{ case.description }}", .enabled(if: RUNALL))
{% endif -%}
func test{{ case.description|camelCase }}() {
#expect(throws: Never.self) {
let actual = try {{ exercise|camelCase }}.squareRoot({{ case.input.radicand }})
#expect(actual == {{ case.expected }})
}
}
{% endfor -%}
}
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/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// swift-tools-version:6.0

import PackageDescription

let package = Package(
name: "SquareRoot",
products: [
.library(
name: "SquareRoot",
targets: ["SquareRoot"])
],
dependencies: [],
targets: [
.target(
name: "SquareRoot",
dependencies: []),
.testTarget(
name: "SquareRootTests",
dependencies: ["SquareRoot"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct SquareRoot {
// Write your code for the 'SquareRoot' exercise here.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Foundation
import Testing

@testable import SquareRoot

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct SquareRootTests {

@Test("root of 1")
func testRootOf1() {
#expect(throws: Never.self) {
let actual = try SquareRoot.squareRoot(1)
#expect(actual == 1)
}
}

@Test("root of 4", .enabled(if: RUNALL))
func testRootOf4() {
#expect(throws: Never.self) {
let actual = try SquareRoot.squareRoot(4)
#expect(actual == 2)
}
}

@Test("root of 25", .enabled(if: RUNALL))
func testRootOf25() {
#expect(throws: Never.self) {
let actual = try SquareRoot.squareRoot(25)
#expect(actual == 5)
}
}

@Test("root of 81", .enabled(if: RUNALL))
func testRootOf81() {
#expect(throws: Never.self) {
let actual = try SquareRoot.squareRoot(81)
#expect(actual == 9)
}
}

@Test("root of 196", .enabled(if: RUNALL))
func testRootOf196() {
#expect(throws: Never.self) {
let actual = try SquareRoot.squareRoot(196)
#expect(actual == 14)
}
}

@Test("root of 65025", .enabled(if: RUNALL))
func testRootOf65025() {
#expect(throws: Never.self) {
let actual = try SquareRoot.squareRoot(65025)
#expect(actual == 255)
}
}
}