MMilad

Writing

2 min read

Sum square difference

Milad Ezzat portrait

Milad Ezzat

@miladezzat12

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))