Kevin Silberberg
  • Home
  • Photos
  • Study

Sum Square Difference

Problem 6

Author

Kevin Silberberg

Published

July 14, 2025

Problem Definition

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

\[1^2 + 2^2 + \cdots + 10^2 = 385 \tag{1}\]

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

\[(1 + 2 + \cdots + 10)^2 = 55^2 = 3025 \tag{2}\]

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.

Solution

In julia we can create a vector of the first \(N\) natural numbers by collecting a range. We can then apply a function to that collection by broadcasting. This uses “Single Instruction Multiple Data” or SIMD compiler optimizations to efficiently solve the problem.

"""
N :: Integer, the first N natural numbers
"""
function sum_square_difference(N::Int)
    numbers = collect(1:N)
    return sum(numbers)^2 - sum(numbers.^2)
end
Main.Notebook.sum_square_difference

Lets check the implementation with the provided solution to see if the function works as expected.

sum_square_difference(10)
2640

Perfect! So the correct answer is,

sum_square_difference(100)
25164150

© Copyright 2025 Kevin Silberberg. Except where otherwise noted, all text and images licensed CC-BY-NC 4.0.