- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Difference between sum of square and square of sum in JavaScript

We are required to write a JavaScript function that takes a number, say n, as the one and the only input.

The function should −

Calculate the sum of squares of the first n natural numbers.

Calculate the square of sums of first n natural numbers.

Return the absolute difference between the both figures obtained.

For example: If n = 5;

Then,

sum of squares = 1 + 4 + 9 + 16 + 25 = 55 square of sums = 15 * 15 = 225

Hence, output = 225 − 55 = 170

## Example

The code for this will be −

const squareDifference = (num = 1) => { let x = 0; let y = 0; let i = 0; let j = 0; // function to compute the sum of squares (function sumOfSquares() { while (i <= num) { x += Math.pow(i, 2); i++; } return x; }()); // function to compute the square of sums (function squareOfSums() { while (j <= num) { y += j; j++; } y = Math.pow(y, 2); return y; }()); // returning the absolute difference return Math.abs(y − x); }; console.log(squareDifference(1)); console.log(squareDifference(5)); console.log(squareDifference(10)); console.log(squareDifference(15));

## Output

And the output in the console will be −

0 170 2640 13160

- Related Articles
- Difference between sum of the squares of and square of sum first n natural numbers.
- Squared and square rooted sum of numbers of an array in JavaScript
- Sum of Square Numbers in C++
- Integers have sum of squared divisors as perfect square in JavaScript
- Sum of square of first n odd numbers
- The sum of two numbers is 1000 and the difference between their square is 256000. Find the numbers.
- Difference between sum and product of an array in JavaScript
- Sum of square-sums of first n natural numbers
- Difference between product and sum of digits of a number in JavaScript
- A square matrix as sum of symmetric and skew-symmetric matrix ?
- Building an array of specific size with consecutive element sum being perfect square in JavaScript
- C++ Range Sum Queries and Update with Square Root
- Print maximum sum square sub-matrix of given size in C Program.
- Write true (T) or false (F) for the following statements.(i) The number of digits in a square number is even.(ii) The square of a prime number is prime.(iii) The sum of two square numbers is a square number.(iv) The difference of two square numbers is a square number.(v) The product of two square numbers is a square number.(vi) No square number is negative.(vii) There is not square number between 50 and 60.(viii) There are fourteen square numbers upto 200.
- ASCII sum difference of strings in JavaScript

Advertisements