n = 100 squareOfSum = sumOfSquares = 0 squareOfSum = 0 # based on a simple internet search # I found you can find the sum # of an arithmetic series with # Sn = n/2(a1 + an) squareOfSum = n/2 * (1 + n) squareOfSum = squareOfSum ** 2 #with another internet search #I found you can get the sum of squares #with the following # ((n)(n+1)(2n+1)) / 6 sumOfSquares = (n * (n + 1) * (2 * n + 1)) / 6 #get the difference between the two difference = squareOfSum - sumOfSquares print "The square of the sum is %d" % (squareOfSum,) print "The sum of squares is %d" % (sumOfSquares,) print "The difference is %d" % (difference,) #the following code solves it the #more obvious way but just #adding up the numbers in a loop #requires no math prowess, but #it's slower squareOfSum = 0 sumOfSquares = 0 i = 1 while i <= n: sumOfSquares += i**2 squareOfSum += i i += 1 squareOfSum = squareOfSum ** 2 difference = squareOfSum - sumOfSquares print "The square of the sum is %d" % (squareOfSum,) print "The sum of squares is %d" % (sumOfSquares,) print "The difference is %d" % (difference,)