Project Euler: Problem 6

https://projecteuler.net/problem=6

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 = 552 = 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.


In [3]:
# recursive function, just for fun
def ssQ(n):
    if n > 1: 
        return n**2 + ssQ(n-1)
    else:
        return 1

def sumSq(n):
    return sum(range(1,n+1))**2
    
def diff(n):
    return sumSq(n) - ssQ(n)
    
print([diff(i) for i in range(1,101)])


[0, 4, 22, 70, 170, 350, 644, 1092, 1740, 2640, 3850, 5434, 7462, 10010, 13160, 17000, 21624, 27132, 33630, 41230, 50050, 60214, 71852, 85100, 100100, 117000, 135954, 157122, 180670, 206770, 235600, 267344, 302192, 340340, 381990, 427350, 476634, 530062, 587860, 650260, 717500, 789824, 867482, 950730, 1039830, 1135050, 1236664, 1344952, 1460200, 1582700, 1712750, 1850654, 1996722, 2151270, 2314620, 2487100, 2669044, 2860792, 3062690, 3275090, 3498350, 3732834, 3978912, 4236960, 4507360, 4790500, 5086774, 5396582, 5720330, 6058430, 6411300, 6779364, 7163052, 7562800, 7979050, 8412250, 8862854, 9331322, 9818120, 10323720, 10848600, 11393244, 11958142, 12543790, 13150690, 13779350, 14430284, 15104012, 15801060, 16521960, 17267250, 18037474, 18833182, 19654930, 20503280, 21378800, 22282064, 23213652, 24174150, 25164150]

In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.