Overview

 

Question

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

\[1^2 + 2^2 + ... + 10^2 = 385\]

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

\[(1 + 2 + ... + 10)^2 = 55^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.

click for answer

25164150

Solutions

 

Ruby

 square_sum.rb https://raw.github.com/addamh/euler/master/006/square_sum.rb download
#!/usr/bin/env ruby

range = 1..100
sum_of_squares = range.inject(0) { |sum, i| sum + i ** 2 }
square_of_sum  = range.inject(0) { |sum, i| sum + i } ** 2
puts square_of_sum - sum_of_squares
$ time ruby square_sum.rb
real	0m0.023s
user	0m0.019s
sys	0m0.003s