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

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

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 wich 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 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 specfic 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.

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.

```
extern crate 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!(25502500, 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!(338350, 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!(25164150, squares::difference(100));
}
```

```
pub fn square_of_sum(x: isize) -> isize {
let mut sum = 0;
for i in 0..x+1 {
sum += i;
}
sum.pow(2)
}
pub fn sum_of_squares(x: isize) -> isize {
let mut sum = 0;
for i in 0..x+1 {
sum += i.pow(2);
}
sum
}
pub fn difference(x: isize) -> isize {
square_of_sum(x) - sum_of_squares(x)
}
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

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

## Community comments