-
-
Notifications
You must be signed in to change notification settings - Fork 28
square root exercise added #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "authors": ["therealowenrees"], | ||
| "files": { | ||
| "solution": [ | ||
| "src/SquareRoot.re" | ||
| ], | ||
| "test": [ | ||
| "__tests__/SquareRoot_test.re" | ||
| ], | ||
| "example": [ | ||
| ".meta/src/Example.re" | ||
| ], | ||
| "editor": [ | ||
| "src/SquareRoot.rei" | ||
| ] | ||
| }, | ||
| "blurb": "Given a natural radicand, return its square root.", | ||
| "source": "wolf99", | ||
| "source_url": "https://github.com/exercism/problem-specifications/pull/1582" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| let squareRoot = n => { | ||
| let radicand = float_of_int(n); | ||
| let rec aux = guess => { | ||
| let next = 0.5 *. (guess +. radicand /. guess); | ||
| switch (abs_float(next -. guess)) { | ||
| | diff when diff < 0.0001 => int_of_float(next) | ||
| | _ => aux(next) | ||
| }; | ||
| }; | ||
| aux(radicand /. 2.0); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
24 changes: 24 additions & 0 deletions
24
exercises/practice/square-root/__tests__/SquareRoot_test.re
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| open Jest; | ||
| open Expect; | ||
| open SquareRoot; | ||
|
|
||
| describe("Square Root", () => { | ||
| test("root of 1", () => | ||
| expect(squareRoot(1)) |> toEqual(1) | ||
| ); | ||
| test("root of 4", () => | ||
| expect(squareRoot(4)) |> toEqual(2) | ||
| ); | ||
| test("root of 25", () => | ||
| expect(squareRoot(25)) |> toEqual(5) | ||
| ); | ||
| test("root of 81", () => | ||
| expect(squareRoot(81)) |> toEqual(9) | ||
| ); | ||
| test("root of 196", () => | ||
| expect(squareRoot(196)) |> toEqual(14) | ||
| ); | ||
| test("root of 65025", () => | ||
| expect(squareRoot(65025)) |> toEqual(255) | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // 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 | ||
| // BuckleScript comes with its own parser for bsconfig.json, which is normal JSON, with the extra support of comments and trailing commas. | ||
| { | ||
| "name": "square-root", | ||
| "version": "0.1.0", | ||
| "sources": [ | ||
| { | ||
| "dir" : "src", | ||
| "subdirs" : true | ||
| }, | ||
| { | ||
| "dir": "__tests__", | ||
| "type": "dev" | ||
| } | ||
| ], | ||
| "package-specs": { | ||
| "module": "commonjs", | ||
| "in-source": true | ||
| }, | ||
| "suffix": ".bs.js", | ||
| "bs-dependencies": [ | ||
| // 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. | ||
| ], | ||
| "bs-dev-dependencies": ["@glennsl/bs-jest"], | ||
| "warnings": { | ||
| "error" : "+101" | ||
| }, | ||
| "namespace": true, | ||
| "refmt": 3 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "name": "exercism-reasonml", | ||
| "version": "0.1.0", | ||
| "scripts": { | ||
| "build": "bsb -make-world", | ||
| "start": "bsb -make-world -w", | ||
| "clean": "bsb -clean-world", | ||
| "test": "jest --watchAll --env=node", | ||
| "test:ci": "jest --ci --bail --no-cache --env=node" | ||
| }, | ||
| "keywords": [ | ||
| "BuckleScript" | ||
| ], | ||
| "author": "", | ||
| "license": "MIT", | ||
| "devDependencies": { | ||
| "@glennsl/bs-jest": "^0.7.0", | ||
| "bs-platform": "^9.0.2" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let squareRoot = (_) => failwith = "squareRoot function not implemented" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let squareRoot: int => int; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.