ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Jul 13 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.

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

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
```

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

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
```

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.

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.

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

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

```
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 {
(0..=n).collect::<Vec<u32>>().iter().sum::<u32>().pow(2)
}
pub fn sum_of_squares(n: u32) -> u32 {
let mut sum: u32 = 0;
for i in 0..=n {
sum += i.pow(2);
}
sum
}
pub fn difference(n: u32) -> u32 {
square_of_sum(n) - sum_of_squares(n)
}
```

The square-of-sums one liner transforms a std::ops::Range into a vector. I couldn't figure out how to go straight from a range to an iterator, so I went to a vector first. After that we can run sum() on the iterator and then square it with pow(2).

The sum-of-squares function is just a simple for loop, which I think is more idiomatic. I couldn't get this one as a one liner as, for some reason, the for_each function on my created iterator was expecting a () closure, instead of the u32 I was passing it. Weird.

Level up your programming skills with 3,424 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments