Visit the difference-of-squares exercise on Exercism to read the full instructions and download the exercise files.
Dig Deeper
Use LINQ
LINQ
using System.Linq;
public static class DifferenceOfSquares
{
public static int CalculateSquareOfSum(int max) =>
Square(Enumerable.Range(1, max).Sum());
public static int CalculateSumOfSquares(int max) =>
Enumerable.Range(1, max).Sum(Square);
public static int CalculateDifferenceOfSquares(int max) =>
CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
private static int Square(int number) => number * number;
}
Before we start, let’s define a helper method to square a number:
private static int Square(int number) => number * number;
Having this method will allow us to write more concise code later on.
Method: CalculateSquareOfSum()
The square of the sum is defined as: sum all numbers from 1 to the provided max, and then square that number.
We can get an enumerable for all the numbers using LINQ’s Enumerable.Range() method:
Enumerable.Range(1, max)
Summing these numbers is a simple matter of calling the Sum() method on the enumerable:
Enumerable.Range(1, max).Sum()
Finally, we can pass this sum to our Square() helper method to calculate the correct value.
public static int CalculateSquareOfSum(int max) =>
Square(Enumerable.Range(1, max).Sum());
Method: CalculateSumOfSquares()
The calculate the sum of the squares, we once again start out with creating an enumerable of the numbers from 1 to max:
Enumerable.Range(1, max).Sum()
We then need to square these numbers, which we can do using the Select() method:
Enumerable.Range(1, max).Select(n => Square(n))
We can then call Sum() to return the correct value:
public static int CalculateSumOfSquares(int max) =>
Enumerable.Range(1, max).Select(n => Square(n)).Sum();
Shortening
There are two things we can do to shorten this method.
The first is that instead of doing Select() and then Sum(), we can use the overload of the Sum() method that takes a lambda (this will have the exact same effect):
public static int CalculateSumOfSquares(int max) =>
Enumerable.Range(1, max).Sum(n => Square(n));
And finally, as the lambda used in the Select() method just passes the argument to a function, we can use a method group to shorten this to:
public static int CalculateSumOfSquares(int max) =>
Enumerable.Range(1, max).Sum(Square);
Method: CalculateDifferenceOfSquares()
The CalculateDifferenceOfSquares() method is nothing more than calling the two methods we just created:
public static int CalculateDifferenceOfSquares(int max) =>
CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
Use a for statement
for statement
public static class DifferenceOfSquares
{
public static int CalculateSquareOfSum(int max)
{
var sum = 0;
for (var i = 1; i <= max; i++)
sum += i;
return sum * sum;
}
public static int CalculateSumOfSquares(int max)
{
var sumOfSquares = 0;
for (var i = 1; i <= max; i++)
sumOfSquares += i * i;
return sumOfSquares;
}
public static int CalculateDifferenceOfSquares(int max) =>
CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
}
Method: CalculateSquareOfSum()
The square of the sum is defined as: sum all numbers from 1 to the provided max, and then square that number.
Let’s start by defining a variable to hold the sum:
var sum = 0;
We can then iterate over the number using a for statement and add each number to the sum:
for (var i = 1; i <= max; i++)
sum += i;
The last step is to run the square of the sum:
return sum * sum;
Method: CalculateSumOfSquares()
The calculate the sum of the squares, we once again start out with creating an enumerable of the numbers from 1 to max:
Let’s start by defining a variable to hold the sum:
var sumOfSquares = 0;
We can then iterate over the number using a for statement and add each number’s square to the sum:
for (var i = 1; i <= max; i++)
sumOfSquares += i * i;
The last step is to run the sum of the squares:
return sumOfSquares;
Method: CalculateDifferenceOfSquares()
The CalculateDifferenceOfSquares() method is nothing more than calling the two methods we just created:
public static int CalculateDifferenceOfSquares(int max) =>
CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
Use math
Math
public static class DifferenceOfSquares
{
public static int CalculateSquareOfSum(int max)
{
var sum = max * (max + 1) / 2;
return sum * sum;
}
public static int CalculateSumOfSquares(int max) =>
(max * (max + 1) * ((max * 2) + 1)) / 6;
public static int CalculateDifferenceOfSquares(int max) =>
CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
}
Method: CalculateSquareOfSum()
The square of the sum is defined as: sum all numbers from 1 to the provided max, and then square that number.
The formula to calculate the sum of numbers 1 to max is: max * (max + 1) / 2.
We can directly convert this to code and then return its square:
var sum = max * (max + 1) / 2;
return sum * sum;
Method: CalculateSumOfSquares()
The calculate the sum of the squares, we can use the following formula: (max * (max + 1) * ((max * 2) + 1)) / 6.
As this has the summation and squaring built-in, we can directly return this formula in code:
public static int CalculateSumOfSquares(int max) =>
(max * (max + 1) * ((max * 2) + 1)) / 6;
Method: CalculateDifferenceOfSquares()
The CalculateDifferenceOfSquares() method is nothing more than calling the two methods we just created:
public static int CalculateDifferenceOfSquares(int max) =>
CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
Source: Exercism csharp/difference-of-squares