"""
N :: Integer, the first N natural numbers
"""
function sum_square_difference(N::Int)
= collect(1:N)
numbers return sum(numbers)^2 - sum(numbers.^2)
end
Main.Notebook.sum_square_difference
Problem 6
Kevin Silberberg
July 14, 2025
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.
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.
Perfect! So the correct answer is,