🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # ybonnel's solution

## to Difference Of Squares in the Rust Track

Published at Sep 24 2020 · 0 comments
Instructions
Test suite
Solution

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is 1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.

You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged. Finding the best algorithm for the problem is a key skill in software engineering.

## Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

## Writing the Code

Execute the tests with:

``````\$ cargo test
``````

All but the first test have been ignored. After you get the first test to pass, open the tests source file which is located in the `tests` directory and remove the `#[ignore]` flag from the next test and get the tests to pass again. Each separate test is a function with `#[test]` flag above it. Continue, until you pass every test.

If you wish to run all ignored tests without editing the tests source file, use:

``````\$ cargo test -- --ignored
``````

To run a specific test, for example `some_test`, you can use:

``````\$ cargo test some_test
``````

If the specific test is ignored use:

``````\$ cargo test some_test -- --ignored
``````

## Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

``````cargo fmt
``````

To see, if your solution contains some common ineffective use cases, inside the solution directory use

``````cargo clippy --all-targets
``````

## Submitting the solution

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.

## Feedback, Issues, Pull Requests

The 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!

If you want to know more about Exercism, take a look at the contribution guide.

## Source

Problem 6 at Project Euler http://projecteuler.net/problem=6

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### difference-of-squares.rs

``````use difference_of_squares as squares;

#[test]
fn test_square_of_sum_1() {
assert_eq!(1, squares::square_of_sum(1));
}

#[test]
#[ignore]
fn test_square_of_sum_5() {
assert_eq!(225, squares::square_of_sum(5));
}

#[test]
#[ignore]
fn test_square_of_sum_100() {
assert_eq!(25_502_500, squares::square_of_sum(100));
}

#[test]
#[ignore]
fn test_sum_of_squares_1() {
assert_eq!(1, squares::sum_of_squares(1));
}

#[test]
#[ignore]
fn test_sum_of_squares_5() {
assert_eq!(55, squares::sum_of_squares(5));
}

#[test]
#[ignore]
fn test_sum_of_squares_100() {
assert_eq!(338_350, squares::sum_of_squares(100));
}

#[test]
#[ignore]
fn test_difference_1() {
assert_eq!(0, squares::difference(1));
}

#[test]
#[ignore]
fn test_difference_5() {
assert_eq!(170, squares::difference(5));
}

#[test]
#[ignore]
fn test_difference_100() {
assert_eq!(25_164_150, squares::difference(100));
}``````
``````pub fn square_of_sum(n: u32) -> u32 {
let sum: u32 = (1..=n).sum();
sum * sum
}

pub fn sum_of_squares(n: u32) -> u32 {
(1..=n).map(|n| n * n).sum()
}

pub fn difference(n: u32) -> u32 {
square_of_sum(n) - sum_of_squares(n)
}``````