Formula

Difference of Squares
Difference of Squares in Java
class DifferenceOfSquaresCalculator {

    int computeSquareOfSumTo(int input) {
        int sum = (input * (input + 1)) / 2;
        return sum * sum;
    }

    int computeSumOfSquaresTo(int input) {
        return (input * (input + 1) * ((input * 2) + 1)) / 6;
    }

    int computeDifferenceOfSquares(int input) {
        return computeSquareOfSumTo(input) - computeSumOfSquaresTo(input);
    }

}

In this solution a formula is used to solve the computeSquareOfSumTo and computeSumOfSquaresTo functions.

At the time of this writing the instructions state:

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.

It is fine to search for an algorithm on the internet. This is also sometimes referred to as "Google is your friend", although you don't have to search with Google.

It is okay if you don't understand how the algorithm works. What is important is that it obviously is not introducing malware and that it passes the tests.

Note that this avoids using Math.pow() in computeSquareOfSumTo, since multiplying a value by itself is usually more efficient than type-casting from intto double back to int.

15th May 2024 · Found it useful?