Created
August 30, 2013 00:55
-
-
Save broguinn/6385213 to your computer and use it in GitHub Desktop.
difference_sum_squares
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def difference_sum_square(number) | |
sum_squared = 0 | |
number.times do |num| | |
sum_squared += num + 1 | |
end | |
sum_squared = sum_squared ** 2 | |
square_sums = 0 | |
number.times do |num| | |
square_sums += (num + 1) ** 2 | |
end | |
sum_squared - square_sums | |
end | |
puts difference_sum_square(100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'difference_sum_squares' | |
describe 'difference_sum_square' do | |
it 'returns the difference between 1 squared and the sum of 1, sqaured' do | |
difference_sum_square(1).should eq 0 | |
end | |
it 'returns the difference for the number 2' do | |
difference_sum_square(2).should eq 4 | |
difference_sum_square(10).should eq 2640 | |
end | |
it 'returns the difference for the number 100' do | |
difference_sum_square(100).should eq 25164150 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment