Euler problem #6
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
----
We don't actually need a computer to solve this problem, but why not?
The formula for the sum of the squares of the first n natural numbers is: n*(n+1)*(2n+1)/6 .
The formula for the sum of the first n natural numbers is: n(n+1)/2 .
We can write this in ruby:
def sumToN(n)
(n*(n+1))/2
end
def sumOfSquares(n)
(n*(n+1)*(2*n+1))/6
end
puts sumToN(100)**2 - sumOfSquares(100)
Note that you don't need an explicit return statement. The last expression in any function will be returned by default.