freeCodeCamp/curriculum/challenges/espanol/10-coding-interview-prep/project-euler/problem-6-sum-square-differ...

1.7 KiB
Raw Blame History

id title challengeType forumTopicId dashedName
5900f3721000cf542c50fe85 Problem 6: Sum square difference 5 302171 problem-6-sum-square-difference

--description--

The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.

Find the difference between the sum of the squares of the first n natural numbers and the square of the sum.

--hints--

sumSquareDifference(10) should return a number.

assert(typeof sumSquareDifference(10) === 'number');

sumSquareDifference(10) should return 2640.

assert.strictEqual(sumSquareDifference(10), 2640);

sumSquareDifference(20) should return 41230.

assert.strictEqual(sumSquareDifference(20), 41230);

sumSquareDifference(100) should return 25164150.

assert.strictEqual(sumSquareDifference(100), 25164150);

--seed--

--seed-contents--

function sumSquareDifference(n) {

  return true;
}

sumSquareDifference(100);

--solutions--

const sumSquareDifference = (number)=>{
  let squareOfSum = Math.pow(sumOfArithmeticSeries(1,1,number),2);
  let sumOfSquare = sumOfSquareOfNumbers(number);
 return squareOfSum - sumOfSquare;
}

function sumOfArithmeticSeries(a,d,n){
  return (n/2)*(2*a+(n-1)*d);
}

function sumOfSquareOfNumbers(n){
 return (n*(n+1)*(2*n+1))/6;
}