Skip to content

Instantly share code, notes, and snippets.

@abdollar
Created December 13, 2011 03:08
Show Gist options
  • Save abdollar/1470338 to your computer and use it in GitHub Desktop.
Save abdollar/1470338 to your computer and use it in GitHub Desktop.
Find the difference between the sum of the squares and the square of the sum
#The sum of the squares of the first ten natural numbers is,
#12 + 22 + ... + 102 = 385
#The square of the sum of the first ten natural numbers is,
#(1 + 2 + ... + 10)2 = 552 = 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
ruby-1.9.2-p180 :004 > (1..10).to_a.reduce(:+)**2 - (1..10).to_a.map { |x| x**2 }.reduce(:+)
=> 2640
ruby-1.9.2-p180 :005 > (1..100).to_a.reduce(:+)**2 - (1..100).to_a.map { |x| x**2 }.reduce(:+)
=> 25164150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment