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
3 changes: 3 additions & 0 deletions bin/auto-sync.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ etl
flatten-array
flower-field
food-chain
game-of-life
gigasecond
grade-school
grains
Expand Down Expand Up @@ -68,6 +69,7 @@ rail-fence-cipher
raindrops
resistor-color
resistor-color-duo
resistor-color-trio
reverse-string
rna-transcription
robot-name
Expand All @@ -83,6 +85,7 @@ series
sieve
space-age
spiral-matrix
square-root
state-of-tic-tac-toe
strain
sublist
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,14 @@
"strings"
]
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "d75c009b-ad65-455f-a824-ed61c8797cc4",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "strain",
"name": "Strain",
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.
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": [
"resu-xuniL"
],
"files": {
"solution": [
"SquareRoot.php"
],
"test": [
"SquareRootTest.php"
],
"example": [
".meta/example.php"
]
},
"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/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

function squareRoot(int $number): int
{
$squareRoot = $number;

while (pow($squareRoot, 2) > $number) {
$squareRoot--;
}

return $squareRoot;
}
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"
30 changes: 30 additions & 0 deletions exercises/practice/square-root/SquareRoot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

function squareRoot(int $number): int
Comment thread
mk-mxp marked this conversation as resolved.
{
throw new \BadMethodCallException(sprintf('Implement the %s function', __FUNCTION__));
}
76 changes: 76 additions & 0 deletions exercises/practice/square-root/SquareRootTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\TestDox;

class SquareRootTest extends TestCase
{
public static function setUpBeforeClass(): void
{
require_once 'SquareRoot.php';
}

Comment thread
resu-xuniL marked this conversation as resolved.
#[TestDox('does not use or mention PHP square root functions')]
public function testDoesNotUseOrMentionPhpSquareRootFunctions(): void
{
$code = file_get_contents(__DIR__ . '/SquareRoot.php');

$this->assertStringNotContainsString('sqrt', $code, 'Please do not use the word "sqrt" anywhere in your code!');
}

/**
* uuid: 9b748478-7b0a-490c-b87a-609dacf631fd
*/
#[TestDox('root of 1')]
public function testRootOfOne(): void
{
$this->assertEquals(1, squareRoot(1));
}

/**
* uuid: 7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb
*/
#[TestDox('root of 4')]
public function testRootOfFour(): void
{
$this->assertEquals(2, squareRoot(4));
}

/**
* uuid: 6624aabf-3659-4ae0-a1c8-25ae7f33c6ef
*/
#[TestDox('root of 25')]
public function testRootOfTwentyFive(): void
{
$this->assertEquals(5, squareRoot(25));
}

/**
* uuid: 93beac69-265e-4429-abb1-94506b431f81
*/
#[TestDox('root of 81')]
public function testRootOfEightyOne(): void
{
$this->assertEquals(9, squareRoot(81));
}

/**
* uuid: fbddfeda-8c4f-4bc4-87ca-6991af35360e
*/
#[TestDox('root of 196')]
public function testRootOfOneHundredNinetySix(): void
{
$this->assertEquals(14, squareRoot(196));
}

/**
* uuid: c03d0532-8368-4734-a8e0-f96a9eb7fc1d
*/
#[TestDox('root of 65025')]
public function testRootOfSixtyFiveThousandTwentyFive(): void
{
$this->assertEquals(255, squareRoot(65025));
}
}
Loading