Problem 6

Problem

The sum of the squares of the first ten natural numbers is \(1^2+ 2 ^2+ \dots + 10^2 = 385.\)

The square of the sum of the first ten natural numbers is \((1+ 2+ \dots + 10)^2 = 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.

Julia

function p6()
  sum(1:100)^2 - sum((1:100).^2)
end;
p6()
25164150
using BenchmarkTools;
@benchmark p6()
BenchmarkTools.Trial: 10000 samples with 983 evaluations.
 Range (minmax):   41.003 ns 1.133 μs   GC (min … max):  0.00% … 83.55%
 Time  (median):      84.259 ns               GC (median):     0.00%
 Time  (mean ± σ):   104.053 ns ± 88.673 ns   GC (mean ± σ):  10.16% ± 10.64%
     █▄▂▂                                                   ▂
  ▅▅████████▇▇▇▆▆▅▅▄▅▄▃▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▃▃▃▃▄▅▄▄▄▆▅▆▆▇▆▅ █
  41 ns         Histogram: log(frequency) by time       689 ns <
 Memory estimate: 896 bytes, allocs estimate: 1.