 # epequeno's solution

## to Difference Of Squares in the Rust Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

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

## 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-square.rs

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