Now, onto the problem!

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.

This one was a good one to roadmap out before I got started. Once I broke it down into a few pieces, it was also not super complicated to solve. So I know I had to find the sum of the squares. And then, the square of the sums. And finally, the difference between the two. Breaking it up into the three pieces also lent itself nicely to writing tests because I wrote tests for each of those three pieces and then a test set for the numbers up to 10 and a test set for the numbers up to 100. Here are the tests:

require 'problem6/problem6' describe 'up to 10' do it "finds the sum of the square of the first ten numbers" do expect(Problem6.sum(1..10)).to eq 385 end it "finds the square of the sum of the first ten numbers" do expect(Problem6.square(1..10)).to eq 3025 end it "finds the difference between the sum of the square and the square of the sums" do expect(Problem6.difference(1..10)).to eq 2640 end end describe 'up to 100' do it "finds the sum of the square of the first one hundred numbers" do expect(Problem6.sum(1..100)).to eq 338350 end it "finds the square of the sum of the first one hundred numbers" do expect(Problem6.square(1..100)).to eq 25502500 end it "finds the difference between the sum of the square and the square of the sums" do expect(Problem6.difference(1..100)).to eq 25164150 end end

And here's the code. You'll see each of the three pieces and how they work together. And you actually don't even need the three pieces. (Looking at it again now as I post, I'm seeing that the question only asks for the different so you don't even need to define the sum and the square separately.) For me, when I originally did this, it was easiest for me to define each to visualize it better but now I see that was unnecessary and the final piece is really the only thing I need.

module Problem6 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 puts Problem6.difference(1..100) end

If you wanted to keep each of the three parts, this is what the code would look like:

module Problem6 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 = square(number_set)-sum(number_set) return difference end puts Problem6.difference(1..100) end

The primary difference is that, instead of taking the information for the difference and repeated all that code, you are taking the result of the sum method and the result of the square method to find the difference.

## No comments:

## Post a Comment