0

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.

Euler Solution


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.

ClearlyQ
  • 31
  • 2
  • What incorrect answer does you program give? – RBarryYoung Sep 19 '21 at 22:22
  • 1
    '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?' Yes, a function must always return the defined type –  Sep 19 '21 at 22:23
  • @RBarryYoung It fails the test case, for example, if the parameter (max) value is 5, the expected output is 0. But my method returns 0. Similarly, all the the other methods also fail the test cases. – ClearlyQ Sep 19 '21 at 22:27
  • 2
    `int squareOfSum = (int)((1D / 4D) * Math.Pow(max, 2) * Math.Pow(max + 1, 2)); return squareOfSum;`: => `(1 / 4) = 0` (integer division). The same applies to `CalculateSumOfSquares()`. – Jimi Sep 19 '21 at 22:31
  • 2
    What do you think the integer result of `1/4` is (considering that both 1 and 4 are integers). The options boil down to 0 or 1 (since in your head, it's somewhere in between). But, it's obviously less than 1, so that leaves zero (and when you multiply by 0..). Would something like `((max*max) * ((max+1)*(max+1)))/4;` match your formula? No conversions to `double`, no casting to `int`, and the division is delayed to the end when when you know the numerator is a multiple of 4 (by the way, since one of `max` and `max+1` must be even, and the square of an even number is divisible by 4 ...) – Flydog57 Sep 19 '21 at 22:39
  • 1
    I'm going to put on my geezer hat. I still think everyone in a technical field should learn to use a slide rule (and to practice those skills on non-trivial problems). It's not going to help on integer division issues (but it might on overflow issues - by the way, did anyone notice that my code encourages overflow (it calculates (roughly) the four power of an integer before doing the division)). Slide rules make you think about the numbers in ways that counting on your fingers, with a calculator or with a computer doesn't – Flydog57 Sep 19 '21 at 23:09

1 Answers1

1

2 Issues here as mentioned by Jimi in the comments, the integer division multiples it by 0 and well 0 times whatever is 0

That is at (1/6) * max and (1/4) * (int)

So calculating the value inside the functions first as a double and then casting it as an integer resolves this

public static class DifferenceOfSquares
{
    public static int CalculateSquareOfSum(int max)
    {
        return Convert.ToInt32(Math.Pow (max, 2) * Math.Pow(max + 1, 2) * 0.25);
    }

    public static int CalculateSumOfSquares(int max)
    {
        return max * (max + 1) * (2 * (max) + 1) * 1/6;
    }

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

My Output:

enter image description here

Note: Didn't test the third function as it's just a division

Addendum: Simplified the functions as to Flydog's comment, love learning from the best :)