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 [14]:
list = range(101)       #makes a list with numbers 1-100
def sum_of_squares(list):
   count=0              #starting variable
   for x in list:
       count=count+x**2 #squares the number in the range then re defines the stariting variabel
   return count
x=sum_of_squares(list)  #calls sum of squares function
print ("sum of squares=",x)          #prints the sum of the squares
def square_of_sums(list):
   count=0              #staring variable
   for x in list:
       count=count+x    #adds numbers in the range
   square=count**2      #then squares that value
   return square
y=square_of_sums(list)  #calls square of sums function 
print ("square of sums=",y)          # prints the sum of the squares
print ("square of sums-sum of squares=",y-x)      #prints the sum of the squares minus the square of the sums


sum of squares= 338350
square of sums= 25502500
square of sums-sum of squares= 25164150

In [ ]:


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

In [ ]: