Avatar of davearonson

davearonson's solution

to Difference Of Squares in the Ruby Track

Published at Jul 13 2018 · 3 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

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.

Hence 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 is 3025 - 385 = 2640.


For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby difference_of_squares_test.rb

To include color from the command line:

ruby -r minitest/pride difference_of_squares_test.rb

Source

Problem 6 at Project Euler http://projecteuler.net/problem=6

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

difference_of_squares_test.rb

require 'minitest/autorun'
require_relative 'difference_of_squares'

# Common test data version: 1.1.0 7a1108b
class DifferenceOfSquaresTest < Minitest::Test
  def test_square_of_sum_1
    # skip
    assert_equal 1, Squares.new(1).square_of_sum
  end

  def test_square_of_sum_5
    skip
    assert_equal 225, Squares.new(5).square_of_sum
  end

  def test_square_of_sum_100
    skip
    assert_equal 25_502_500, Squares.new(100).square_of_sum
  end

  def test_sum_of_squares_1
    skip
    assert_equal 1, Squares.new(1).sum_of_squares
  end

  def test_sum_of_squares_5
    skip
    assert_equal 55, Squares.new(5).sum_of_squares
  end

  def test_sum_of_squares_100
    skip
    assert_equal 338_350, Squares.new(100).sum_of_squares
  end

  def test_difference_of_squares_1
    skip
    assert_equal 0, Squares.new(1).difference
  end

  def test_difference_of_squares_5
    skip
    assert_equal 170, Squares.new(5).difference
  end

  def test_difference_of_squares_100
    skip
    assert_equal 25_164_150, Squares.new(100).difference
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 4, BookKeeping::VERSION
  end
end
class Squares

  def initialize(how_many)
    @how_many = how_many
  end

  def difference
    square_of_sums - sum_of_squares
  end

  def square_of_sums
    # n*(n+1)/2 is standard formula for sum(1..n)
    (@how_many * (@how_many + 1) / 2) ** 2
  end

  def sum_of_squares
    # from https://en.wikipedia.org/wiki/Square_pyramidal_number
    (2 * @how_many**3 + 3 * @how_many**2 + @how_many) / 6
  end

end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of davearonson

What drove me in the direction of using formulae for the square_of_sums and vice-versa was the performance geek in me. :-) This makes the performance constant-time rather than linear. That was actually the last thing I did after making all the tests pass. Up until then, it was driven by just making the tests pass....

Avatar of fomentia

I really like the formulae! Two thumbs up.

Avatar of davearonson

Thanks! I wasted a good bit of time trying to figure out the second formula, and then Googling it, before I finally realized that the term "square pyramidal number", which I had glossed over in the list of Google hits, perfectly described what I was after.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?