Today I committed to completing #100DaysofCode. The two rules for completing the challenge are that:

  1. I code for at least 1 hour a day, and,
  2. I tweet my progress every day with the #100DaysOfCode hashtag (I'm also going to write about each day's progress here on the blog).

Difference of squares

I've spent about 35 minutes writing a small Ruby app that computes the difference of squares. The exercise is an optional challenge that comes from the Ruby track on Exercism.io.

The goal is to find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

For example, if N=10:

  • The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)² = 55² = 3025.
  • The sum of the squares of the first ten natural numbers is 1² + 2² + ... + 10² = 385.

Assuming the above, the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers would be 2640 (3025 - 385).

The following test suite (difference_of_squares_test.rb) is supplied with the challenge:

require 'minitest/autorun'                                                           
require_relative 'difference_of_squares'                                             
                                                                                     
# Common test data version: 1.2.0 1b6851d                                            
class DifferenceOfSquaresTest < Minitest::Test                                       
  def test_square_of_sum_1                                                           
    assert_equal 1, Squares.new(1).square_of_sum                                     
  end                                                                                
                                                                                     
  def test_square_of_sum_5                                                           
    assert_equal 225, Squares.new(5).square_of_sum                                   
  end                                                                                
                                                                                     
  def test_square_of_sum_100                                                         
    assert_equal 25_502_500, Squares.new(100).square_of_sum                          
  end                                                                                
                                                                                     
  def test_sum_of_squares_1                                                          
    assert_equal 1, Squares.new(1).sum_of_squares                                    
  end                                                                                
                                                                                     
  def test_sum_of_squares_5                                                          
    assert_equal 55, Squares.new(5).sum_of_squares                                   
  end                                                                                
                                                                                     
  def test_sum_of_squares_100                                                        
    assert_equal 338_350, Squares.new(100).sum_of_squares                            
  end                                                                                
                                                                                     
  def test_difference_of_squares_1                                                   
    assert_equal 0, Squares.new(1).difference                                        
  end                                                                                
                                                                                     
  def test_difference_of_squares_5                                                   
    assert_equal 170, Squares.new(5).difference                                      
  end                                                                                
                                                                                     
  def test_difference_of_squares_100                                                 
    assert_equal 25_164_150, Squares.new(100).difference                             
  end                                                                                
end 

And here was my solution (difference_of_squares.rb):

Squares = Struct.new(:number) do                                                     
                                                                                     
  def square_of_sum                                                                  
    range = (1..number).to_a                                                         
    range.sum ** 2                                                                   
  end                                                                                
                                                                                     
  def sum_of_squares                                                                 
    range = (1..number).to_a                                                         
    squares = range.map { |n| n ** 2 }                                               
    squares.sum                                                                      
  end                                                                                
                                                                                     
  def difference                                                                     
    range = (1..number).to_a                                                         
    square_of_sum = range.sum ** 2                                                   
    sum_of_squares = (range.map { |n| n ** 2 }).sum                                  
    square_of_sum - sum_of_squares                                                   
  end                                                                                
end  

If there was a better way I could have done this, please let me know. :)

The only main issue that I encountered with this exercise was that I initially had some trouble correctly writing the equation for determining a square of a number - I was incorrectly calculating it as n ** n instead of n ** 2.  Once I figured that out, the rest was pretty much a breeze. I used IRB to troubleshoot the issue which quickly helped me pinpoint the problem.

Moving on

The rest of the hour I've spent writing this post! But I'm still going to keep coding more today. I'm currently reading Agile Web Development with Rails 6. This book is about learning Rails the way the "Rails core team recommends it" (quoting verbatim from the book's description).

I'm already done with a lot of the first part of the book, which was pretty straightforward since I had done it before (how to install Rails on different operating systems, how to set up Git, etc.). It also covered how to use the rails command to perform various tasks like create a new app (rails new), or a new controller (rails generate controller [name] [actions]). This was done by walking through the creation of an example "Hello World" app, with some ERB tossed in to show how you can do things like display the current time to a site visitor.

Now the book is moving into more detailed explanations of the architecture of a Rails application and the Model-View-Controller (MVC) design pattern that it utilizes... maybe another post. :)