JSFiddle - React, Tailwind, and code Playground

by Gabriel Vazquez

JavaScript

/**
 * 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 one hundred natural numbers and the square of the sum.
 */

const main = (limit) => {
  // 1 = 1 - 3
  // 2 = 4 - 5
  // 3 = 9 - 7
  // 4 = 16 - 9
  // 5 = 25 - 11
  // 6 = 36 - 13
  // 7 = 49 - 15
  // 8 = 64 - 17
  // 9 = 81 - 19
  // 10 = 100 - 21
  // 11 = 121  - 23
  // 12 = 144 - 25
  // 13 = 169 - 27
  // 14 = 196 - 29
  // d = (a^2 - b^2)
  let sumSquares = 0;
  let squareOfSum = 0;
  let difference;
  for (let n = 1; n <= limit; n++) {
    sumSquares += Math.pow(n, 2);
    squareOfSum += n;
  }
  //
  squareOfSum = Math.pow(squareOfSum, 2);
  return sumSquares - squareOfSum;
}

console.log('difference', main(100));