Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

1.1 KiB

title
title
Sum square difference

Problem 6: Sum square difference


Problem Explanation

  • Sum of first n natural numbers can be calculated by using this formula:

    • sum of n numbers
  • Sum of squares of n natural numbers can be calculated by using this formula:

    • sum of n squares
  • We can calculate the values using the above formula and subtract them to get the result.


Solutions

Solution 1 (Click to Show/Hide)
function sumSquareDifference(n) {
  const sumOfN = (n*(n+1))/2;
  const sumOfNSquare = (n*(n+1)*(2*n+1))/6;
  
  //** is exponentaial operator added in ES7, it's equivalent to Math.pow(num, 2)`
  return (sumOfN ** 2) - sumOfNSquare;
}