Sum square difference
The sum of the squares of the first ten natural numbers is,
$$ 1^x1D2 + 2^2 + … + 10^2 = 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
Clearly there are two parts needed to solve the problem. Let’s take a look to each one:
Sum of the squares of N⌗
We will create a function that accepts parameter N, and calculates the sum of squares.
I ran this alone with N = 10 and the results were:
Sum of Squares: 385 Execution Time: 43.542µs
Pretty fast!
With N = 100, the results were:
Sum of Squares: 338350 Execution Time: 29.438µs
It was even faster…
Square of the sum of N⌗
For the second part we’ll do more or less the same but instead we’ll first sum everything and then elevate it.
With N = 10:
Square of Sum of N: 3025 Execution Time: 38.978µs
And with N = 100
Square of Sum of N: 25502500 Execution Time: 39.731µs
~1.3µs of difference… I really like Go.
Finally we’ll substract the later with the former.
Execution Time: 31.696µs
Answer Sum of Squares of N minus Square of Sum of N: 25164150"