artemkorsakov's solution

to Difference Of Squares in the C# Track

Published at Feb 08 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.

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

Submitting Incomplete Solutions

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

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)
{
return (int)Math.Pow(Enumerable.Range(1, max).Sum(), 2);
}

public static int CalculateSumOfSquares(int max)
{
return Enumerable.Range(1, max).Sum(i => i * i);
}

public static int CalculateDifferenceOfSquares(int max)
{
return CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
}
}``````