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

Published at Apr 15 2019
·
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.

This exercise requires you to process a collection of data. You can simplify your code by using LINQ (Language Integrated Query). For more information, see [this page] (https://docs.microsoft.com/en-us/dotnet/articles/standard/using-linq).

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit DifferenceOfSquares.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

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

```
// This file was auto-generated based on version 1.2.0 of the canonical data.
using Xunit;
public class DifferenceOfSquaresTest
{
[Fact]
public void Square_of_sum_1()
{
Assert.Equal(1, DifferenceOfSquares.CalculateSquareOfSum(1));
}
[Fact(Skip = "Remove to run test")]
public void Square_of_sum_5()
{
Assert.Equal(225, DifferenceOfSquares.CalculateSquareOfSum(5));
}
[Fact(Skip = "Remove to run test")]
public void Square_of_sum_100()
{
Assert.Equal(25502500, DifferenceOfSquares.CalculateSquareOfSum(100));
}
[Fact(Skip = "Remove to run test")]
public void Sum_of_squares_1()
{
Assert.Equal(1, DifferenceOfSquares.CalculateSumOfSquares(1));
}
[Fact(Skip = "Remove to run test")]
public void Sum_of_squares_5()
{
Assert.Equal(55, DifferenceOfSquares.CalculateSumOfSquares(5));
}
[Fact(Skip = "Remove to run test")]
public void Sum_of_squares_100()
{
Assert.Equal(338350, DifferenceOfSquares.CalculateSumOfSquares(100));
}
[Fact(Skip = "Remove to run test")]
public void Difference_of_squares_1()
{
Assert.Equal(0, DifferenceOfSquares.CalculateDifferenceOfSquares(1));
}
[Fact(Skip = "Remove to run test")]
public void Difference_of_squares_5()
{
Assert.Equal(170, DifferenceOfSquares.CalculateDifferenceOfSquares(5));
}
[Fact(Skip = "Remove to run test")]
public void Difference_of_squares_100()
{
Assert.Equal(25164150, DifferenceOfSquares.CalculateDifferenceOfSquares(100));
}
}
```

```
ï»¿using System;
using System.Linq;
public static class DifferenceOfSquares
{
public static int CalculateSquareOfSum(int max)
{
int squareOfSum = Enumerable.Range(1, max).Select(x => x).Sum();
return squareOfSum * squareOfSum;
}
public static int CalculateSumOfSquares(int max)
{
int sumOfSquares = Enumerable.Range(1, max).Select(x => x * x).Sum();
return sumOfSquares;
}
public static int CalculateDifferenceOfSquares(int max)
{
return CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
}
}
```

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,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments