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 [1]:
# YOUR CODE HERE


def sum_of_squares(r):
        n = 0
        #loop throu values and square them than add them up
        for i in range(r + 1):
            n += i ** 2
        #return the sum of the squared values
        return n
            
def square_of_sum(r):
    #add all vaues together and define it as q
    q = (( r + 1 ) * r) / 2
    #square q and define it as s
    s = q ** 2
    #return the sum squared
    return s

#take the diffrance of the two previously defined functions
def diffrence(r):
    return square_of_sum(r) - sum_of_squares(r)

#print the values of the functions up to 100
print (sum_of_squares(100))
print (square_of_sum(100))
print (diffrence(100))


338350
25502500.0
25164150.0

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