1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Squares def initialize(num) @num = num end def square_of_sums # Square of sum of 1..n - use triangle number formula ((@num * (@num + 1)) / 2)**2 end def sum_of_squares # Sum of all squares 1..n**2 # Use the sum of squares formula (@num * (@num + 1) * (2*@num + 1)) / 6 end def difference square_of_sums - sum_of_squares end end |

class Squares
def initialize(num)
@num = num
end
def square_of_sums
# Square of sum of 1..n - use triangle number formula
((@num * (@num + 1)) / 2)**2
end
def sum_of_squares
# Sum of all squares 1..n**2
# Use the sum of squares formula
(@num * (@num + 1) * (2*@num + 1)) / 6
end
def difference
square_of_sums - sum_of_squares
end
end

## Comments

Swapped around the difference operation and removed completely unnecessary call to abs.

You're not logged in right now. Please login via GitHub to comment