In [20]:
"""
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
"""
def findSquareOfSum(x):
"""Returns the sum of squares of numbers from 0 up to x."""
return sum([y * y for y in range(x+1)])
def findSumOfSquares(x):
"""Returns the square of the sum of numbers from 0 up to x."""
return sum(range(x+1))**2
def findDiffenrence(x):
"""Returns the difference between the sum of squares and the square of the sum for numbers between 0 and x."""
return findSumOfSquares(x)-findSquareOfSum(x)
In [21]:
findSumOfSquares(10)
Out[21]:
In [22]:
findSquareOfSum(10)
Out[22]:
In [24]:
findDiffenrence(100)
Out[24]: