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 square of the first one hundred natural numbers and the square of the sum.
Let’s solve using Julia
Code
functionSumSquareDifference(n)#' @@name SumSquareDifference#'#' @@description#'#' This function finds the differnce between the sum of the square of the first n natural numbers and the square of the sum#'#' @@arg n: A positive integer n#'#' @@return The differnce between the sum of the square of the first n natural numbers and the square of the sum#'#' @@examples#'#' n = 10#' SumOfSquareDifference(n)sum_of_squares =0sum =0for i =1:n sum_of_squares = sum_of_squares + (i^2) sum = sum + iend square_of_sum = sum^2 difference = square_of_sum - sum_of_squaresreturn difference, square_of_sum, sum_of_squaresend
SumSquareDifference (generic function with 1 method)
Let’s begin by checking the given example of the first 10 natural numbers
Code
SumSquareDifference(10)
(2640, 3025, 385)
Therefore we can see that the difference is 2640 which is the correct solution.
Now let’s find the difference of the sum of the squares of the first one hundred natural numbers and the square of the sum