diff options
| author | Federico Igne <git@federicoigne.com> | 2020-12-26 17:48:38 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch) | |
| tree | 8e39798fcaf27931d91c2088423fd4e97adcfc2d /difference-of-squares | |
| parent | 4e2052c4d792540c2f742b2c2a081b11117ed41d (diff) | |
| download | exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip | |
Move Rust exercises in a subdirectory
Diffstat (limited to 'difference-of-squares')
| -rw-r--r-- | difference-of-squares/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | difference-of-squares/.gitignore | 8 | ||||
| -rw-r--r-- | difference-of-squares/Cargo.toml | 4 | ||||
| -rw-r--r-- | difference-of-squares/README.md | 97 | ||||
| -rw-r--r-- | difference-of-squares/src/lib.rs | 14 | ||||
| -rw-r--r-- | difference-of-squares/tests/difference-of-squares.rs | 46 |
6 files changed, 0 insertions, 170 deletions
diff --git a/difference-of-squares/.exercism/metadata.json b/difference-of-squares/.exercism/metadata.json deleted file mode 100644 index 7e844ee..0000000 --- a/difference-of-squares/.exercism/metadata.json +++ /dev/null | |||
| @@ -1 +0,0 @@ | |||
| 1 | {"track":"rust","exercise":"difference-of-squares","id":"9ad3be19e839435282ee30ce156fdb07","url":"https://exercism.io/my/solutions/9ad3be19e839435282ee30ce156fdb07","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file | ||
diff --git a/difference-of-squares/.gitignore b/difference-of-squares/.gitignore deleted file mode 100644 index db7f315..0000000 --- a/difference-of-squares/.gitignore +++ /dev/null | |||
| @@ -1,8 +0,0 @@ | |||
| 1 | # Generated by Cargo | ||
| 2 | # will have compiled files and executables | ||
| 3 | /target/ | ||
| 4 | **/*.rs.bk | ||
| 5 | |||
| 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
| 7 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
| 8 | Cargo.lock | ||
diff --git a/difference-of-squares/Cargo.toml b/difference-of-squares/Cargo.toml deleted file mode 100644 index 41e2b29..0000000 --- a/difference-of-squares/Cargo.toml +++ /dev/null | |||
| @@ -1,4 +0,0 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2018" | ||
| 3 | name = "difference-of-squares" | ||
| 4 | version = "1.2.0" | ||
diff --git a/difference-of-squares/README.md b/difference-of-squares/README.md deleted file mode 100644 index d940ad5..0000000 --- a/difference-of-squares/README.md +++ /dev/null | |||
| @@ -1,97 +0,0 @@ | |||
| 1 | # Difference Of Squares | ||
| 2 | |||
| 3 | Find the difference between the square of the sum and the sum of the squares of the first N natural numbers. | ||
| 4 | |||
| 5 | The square of the sum of the first ten natural numbers is | ||
| 6 | (1 + 2 + ... + 10)² = 55² = 3025. | ||
| 7 | |||
| 8 | The sum of the squares of the first ten natural numbers is | ||
| 9 | 1² + 2² + ... + 10² = 385. | ||
| 10 | |||
| 11 | Hence the difference between the square of the sum of the first | ||
| 12 | ten natural numbers and the sum of the squares of the first ten | ||
| 13 | natural numbers is 3025 - 385 = 2640. | ||
| 14 | |||
| 15 | You are not expected to discover an efficient solution to this yourself from | ||
| 16 | first principles; research is allowed, indeed, encouraged. Finding the best | ||
| 17 | algorithm for the problem is a key skill in software engineering. | ||
| 18 | |||
| 19 | ## Rust Installation | ||
| 20 | |||
| 21 | Refer to the [exercism help page][help-page] for Rust installation and learning | ||
| 22 | resources. | ||
| 23 | |||
| 24 | ## Writing the Code | ||
| 25 | |||
| 26 | Execute the tests with: | ||
| 27 | |||
| 28 | ```bash | ||
| 29 | $ cargo test | ||
| 30 | ``` | ||
| 31 | |||
| 32 | All but the first test have been ignored. After you get the first test to | ||
| 33 | pass, open the tests source file which is located in the `tests` directory | ||
| 34 | and remove the `#[ignore]` flag from the next test and get the tests to pass | ||
| 35 | again. Each separate test is a function with `#[test]` flag above it. | ||
| 36 | Continue, until you pass every test. | ||
| 37 | |||
| 38 | If you wish to run all ignored tests without editing the tests source file, use: | ||
| 39 | |||
| 40 | ```bash | ||
| 41 | $ cargo test -- --ignored | ||
| 42 | ``` | ||
| 43 | |||
| 44 | To run a specific test, for example `some_test`, you can use: | ||
| 45 | |||
| 46 | ```bash | ||
| 47 | $ cargo test some_test | ||
| 48 | ``` | ||
| 49 | |||
| 50 | If the specific test is ignored use: | ||
| 51 | |||
| 52 | ```bash | ||
| 53 | $ cargo test some_test -- --ignored | ||
| 54 | ``` | ||
| 55 | |||
| 56 | To learn more about Rust tests refer to the [online test documentation][rust-tests] | ||
| 57 | |||
| 58 | Make sure to read the [Modules][modules] chapter if you | ||
| 59 | haven't already, it will help you with organizing your files. | ||
| 60 | |||
| 61 | ## Further improvements | ||
| 62 | |||
| 63 | After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. | ||
| 64 | |||
| 65 | To format your solution, inside the solution directory use | ||
| 66 | |||
| 67 | ```bash | ||
| 68 | cargo fmt | ||
| 69 | ``` | ||
| 70 | |||
| 71 | To see, if your solution contains some common ineffective use cases, inside the solution directory use | ||
| 72 | |||
| 73 | ```bash | ||
| 74 | cargo clippy --all-targets | ||
| 75 | ``` | ||
| 76 | |||
| 77 | ## Submitting the solution | ||
| 78 | |||
| 79 | Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. | ||
| 80 | |||
| 81 | ## Feedback, Issues, Pull Requests | ||
| 82 | |||
| 83 | The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! | ||
| 84 | |||
| 85 | If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). | ||
| 86 | |||
| 87 | [help-page]: https://exercism.io/tracks/rust/learning | ||
| 88 | [modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html | ||
| 89 | [cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html | ||
| 90 | [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html | ||
| 91 | |||
| 92 | ## Source | ||
| 93 | |||
| 94 | Problem 6 at Project Euler [http://projecteuler.net/problem=6](http://projecteuler.net/problem=6) | ||
| 95 | |||
| 96 | ## Submitting Incomplete Solutions | ||
| 97 | It's possible to submit an incomplete solution so you can see how others have completed the exercise. | ||
diff --git a/difference-of-squares/src/lib.rs b/difference-of-squares/src/lib.rs deleted file mode 100644 index 4599733..0000000 --- a/difference-of-squares/src/lib.rs +++ /dev/null | |||
| @@ -1,14 +0,0 @@ | |||
| 1 | /// Implements square of Gauss' formula | ||
| 2 | pub fn square_of_sum(n: u32) -> u32 { | ||
| 3 | (n * (n + 1) / 2).pow(2) | ||
| 4 | } | ||
| 5 | |||
| 6 | /// Implements the formula reported in Wolfram|Alpha at this | ||
| 7 | /// [link](https://www.wolframalpha.com/input/?i=sum+of+squares&assumption=%7B%22F%22%2C+%22SumOfSquaresOfIntegers%22%2C+%22sumlowerlimit%22%7D+-%3E%221%22&assumption=%7B%22F%22%2C+%22SumOfSquaresOfIntegers%22%2C+%22sumupperlimit2%22%7D+-%3E%22n%22) | ||
| 8 | pub fn sum_of_squares(n: u32) -> u32 { | ||
| 9 | n * (1 + n) * (1 + 2 * n) / 6 | ||
| 10 | } | ||
| 11 | |||
| 12 | pub fn difference(n: u32) -> u32 { | ||
| 13 | square_of_sum(n) - sum_of_squares(n) | ||
| 14 | } | ||
diff --git a/difference-of-squares/tests/difference-of-squares.rs b/difference-of-squares/tests/difference-of-squares.rs deleted file mode 100644 index 4a029b3..0000000 --- a/difference-of-squares/tests/difference-of-squares.rs +++ /dev/null | |||
| @@ -1,46 +0,0 @@ | |||
| 1 | use difference_of_squares as squares; | ||
| 2 | |||
| 3 | #[test] | ||
| 4 | fn test_square_of_sum_1() { | ||
| 5 | assert_eq!(1, squares::square_of_sum(1)); | ||
| 6 | } | ||
| 7 | |||
| 8 | #[test] | ||
| 9 | fn test_square_of_sum_5() { | ||
| 10 | assert_eq!(225, squares::square_of_sum(5)); | ||
| 11 | } | ||
| 12 | |||
| 13 | #[test] | ||
| 14 | fn test_square_of_sum_100() { | ||
| 15 | assert_eq!(25_502_500, squares::square_of_sum(100)); | ||
| 16 | } | ||
| 17 | |||
| 18 | #[test] | ||
| 19 | fn test_sum_of_squares_1() { | ||
| 20 | assert_eq!(1, squares::sum_of_squares(1)); | ||
| 21 | } | ||
| 22 | |||
| 23 | #[test] | ||
| 24 | fn test_sum_of_squares_5() { | ||
| 25 | assert_eq!(55, squares::sum_of_squares(5)); | ||
| 26 | } | ||
| 27 | |||
| 28 | #[test] | ||
| 29 | fn test_sum_of_squares_100() { | ||
| 30 | assert_eq!(338_350, squares::sum_of_squares(100)); | ||
| 31 | } | ||
| 32 | |||
| 33 | #[test] | ||
| 34 | fn test_difference_1() { | ||
| 35 | assert_eq!(0, squares::difference(1)); | ||
| 36 | } | ||
| 37 | |||
| 38 | #[test] | ||
| 39 | fn test_difference_5() { | ||
| 40 | assert_eq!(170, squares::difference(5)); | ||
| 41 | } | ||
| 42 | |||
| 43 | #[test] | ||
| 44 | fn test_difference_100() { | ||
| 45 | assert_eq!(25_164_150, squares::difference(100)); | ||
| 46 | } | ||
