Instructions
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.
I'm using the formula below to solve this problem. However, I'm not sure why my code (also below) is although compiling, is not returning the correct test results . I'm assuming its because there's no type conversion on the the fraction part (1/4), which I don't see how to convert.
Since the method is an integer type ("public static int") and the parameter it accepts is an integer, does this mean the operation needs to return an integer? and the (1/4) can't exist in an integer form.
Note: The 3 method headers "public static int ..."are already provided in the exercise.
public static class DifferenceOfSquares
{
public static int CalculateSquareOfSum(int max)
{
int SquareOfSum;
return SquareOfSum = (1/4) * (int) Math.Pow (max, 2) * (int)Math.Pow(max + 1, 2);
}
public static int CalculateSumOfSquares(int max)
{
int SumOfSquares;
return SumOfSquares = (1/6) * max * (max +1)* (2*(max) +1);
}
public static int CalculateDifferenceOfSquares(int max)
{
int Difference;
return Difference = CalculateSquareOfSum (max) - CalculateSumOfSquares (max);
}
}
I'd appreciate any guidance, been stuck on this for a while.