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

# freddie2025's solution

## to Difference Of Squares in the C# Track

Published at Apr 15 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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.

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.

## Hints

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).

## Running the tests

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`

## Further information

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.

## Source

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

### DifferenceOfSquaresTest.cs

``````// 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);
}
}``````
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://exercism.io/tracks/csharp/exercises/difference-of-squares/solutions/2a16be5005ad4b1c81f9794282b901f3", "fetch_time": 1611331564000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2021-04/segments/1610703530835.37/warc/CC-MAIN-20210122144404-20210122174404-00409.warc.gz", "warc_record_offset": 334460036, "warc_record_length": 6646, "token_count": 1006, "char_count": 3940, "score": 3.65625, "int_score": 4, "crawl": "CC-MAIN-2021-04", "snapshot_type": "latest", "language": "en", "language_score": 0.879943311214447, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00046-of-00064.parquet"}