Difference between sum of square and square of sum in JavaScript


In the given problem statement we have to find the difference between sum of square and square of sum with the help of Javascript functionalities. So we will create two functions to do this task.

Understanding the Problem

The problem at hand is to calculate the sum of squares and the square of the sum for a given set of numbers in Javascript programming. The sum of squares specifies the sum of the squares of every number in the given set. On the other hand the square of the sum means summing up all the numbers in the set and then compute its square. Means first we will add all the numbers and then square the final sum.

For example suppose we have an array of numbers as [1, 2, 3], so the sum of squares are (1^2) = 1, (2^2) = 4 and (3^2) = 9 and the sum of all the numbers here is 1 + 4 + 9 = 14, similarly calculate the square of sums 1 + 2 + 3 = 6 and square of 6 is (6 * 6) = 36. So the difference between these two values is 36 - 14 = 22. This is the required result.

Logic for the given Problem

To solve the problem, we will create a function in Javascript, and the code includes three functions to calculate the sum of squares and the square of sums. The sum of squares function will calculate the sum of squares by traversing through the numbers array, and square every number and sum up the squared values. The squares of sum function computes the squares of the sum by traversing the numbers array. And add all the numbers and then square the sum value. Just like the previous function we will calculate the sum of squares to get the squares of each number and then sum all the square values to get the result. In the end we will find the difference between both the values.

Algorithm

Step 1: As we have to calculate the sum of squares to do this task we will create a function to return the sum of squares that accepts a parameter of numbers array in it.

Step 2: In the function we are required to store the values of the sum so create a variable and set it to zero.

Step 3: We are required to iterate the items of the array so using a for loop we will iterate the numbers array to get the squares of each number.

Step 4: For calculating the squares of each number we will use mathematical operations to calculate the squares of each number and sum every square value with the previous sum value.

Step 5: So we will have the sum of all the squares of each number.

Step 6: As we have already discussed that we will have two functions. So create another function for calculating the square of sum and use a parameter of numbers array in it.

Step 7: Just like the above function we will follow those steps in this function also but the difference will be that we are calculating square of sum so first we will calculate the sum of all the numbers present in the array and after having the sum we will find out its square the mathematical formula.

Step 8: So we will have the result of the above function.

Step 9: Declare one more function to show the results of both the functions and calculate the difference of both the values.

Example

//Function to compute sum of squares
function sumOfSquares(nums) {
   let sum = 0;
   for (let i = 0; i < nums.length; i++) {
      sum += nums[i] * nums[i];
   }
   return sum;
}

//Function to compute square of sum
function squareOfSum(nums) {
   let sum = 0;
   for (let i = 0; i < nums.length; i++) {
      sum += nums[i];
   }
   return sum * sum;
}

function check() {
   const nums = [1, 2, 3];
   const sumOfSq = sumOfSquares(nums);
   const sqOfSum = squareOfSum(nums);

   console.log("Sum of the Squares:", sumOfSq);
   console.log("Square of the Sum:", sqOfSum);

   const difference = sqOfSum - sumOfSq;
   console.log("The Difference: ", difference);
}
check();

Output

Sum of the Squares: 14
Square of the Sum: 36
The Difference:  22

Complexity

The time complexity for finding the difference between the sum of squares and the square of sum is O(n) for both the functions. In this n is the number of items present in the numbers array. And the space complexity is constant as O(1). Because we are utilizing the variables to store the results

Conclusion

The solution we have provided is an efficient way for calculating the difference of the sum of squares and the square of sum for the given set of numbers. With the usage of two functions, the code allows us to ensure the calculations can be performed quickly and without any extra memory usage.

Updated on: 14-Aug-2023

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements