2 min read

Sum square difference

Sum square difference

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

Sum of the squares of the first ten natural numbers

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

Square of the sum of the first ten natural numbers

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is

.

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

const sumOfSquares = (from = 1, to = 1) => {
  let sum = 0
  for (let index = from; index <= to; index++) {
    const number = index
    sum += number ** 2
  }

  return sum
}

const squareOfSum = (from = 1, to = 1) => {
  let sum = 0

  for (let index = from; index <= to; index++) {
    sum += index
  }

  return sum ** 2
}

console.log(squareOfSum(1, 100) - sumOfSquares(1, 100))
  • Engineering

    Multiples of 3 or 5

    If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

    1 min read ↗
  • Engineering1 min read ↗