Skip to content

Instantly share code, notes, and snippets.

@Manume
Last active August 29, 2015 14:02
Show Gist options
  • Save Manume/5ec91cc1099fa1343d1e to your computer and use it in GitHub Desktop.
Save Manume/5ec91cc1099fa1343d1e to your computer and use it in GitHub Desktop.
difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
#difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
class Sum
def self.sum(number_set)
sum = (number_set).map { |i| i*i }.reduce(:+)
#put here the sum of squares of the numbers
return sum
end
def self.square(number_set)
square = (number_set).reduce(:+)**2
# put the square of the sum of the numbers
return square
end
def self.difference(number_set)
difference = (number_set).reduce(:+)**2 - (number_set).map { |i| i*i }.reduce(:+)
return difference
end
obj=Sum.new
output=Sum.difference(1..100)
puts"difference between the sum of the squares of the first one hundred natural numbers and the square of the sum is #{output}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment