Skip to content

Instantly share code, notes, and snippets.

@jagipson
Created July 21, 2010 11:50
Show Gist options
  • Save jagipson/484380 to your computer and use it in GitHub Desktop.
Save jagipson/484380 to your computer and use it in GitHub Desktop.
!#/usr/bin/ruby
max = 100
sum = (0..max).inject { |s,n| s + n**2 }
sqr = ((0..max).inject { |s,n| s+n }) ** 2
puts sqr-sum
###### Alternate solution :
#!/usr/bin/ruby
require 'mathn'
n = 100
puts "Difference is #{ (n * (n**2 - 1) * (n / 4 + 1 / 6)).abs }."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment