Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-6-sum-square-difference.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

2.0 KiB
Raw Blame History

id, challengeType, title, forumTopicId
id challengeType title forumTopicId
5900f3721000cf542c50fe85 5 Problem 6: Sum square difference 302171

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.

Instructions

Tests

tests:
  - text: <code>sumSquareDifference(10)</code> should return a number.
    testString: assert(typeof sumSquareDifference(10) === 'number');
  - text: <code>sumSquareDifference(10)</code> should return 2640.
    testString: assert.strictEqual(sumSquareDifference(10), 2640);
  - text: <code>sumSquareDifference(20)</code> should return 41230.
    testString: assert.strictEqual(sumSquareDifference(20), 41230);
  - text: <code>sumSquareDifference(100)</code> should return 25164150.
    testString: assert.strictEqual(sumSquareDifference(100), 25164150);

Challenge Seed

function sumSquareDifference(n) {
  // Good luck!
  return true;
}

sumSquareDifference(100);

Solution

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;
}