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.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
First, I created a list which holds all numbers up to 100.
In [3]:
lst = range(101)
Then I created a new variable, sum_squares, which would hold the sum of the squares, and set it to zero. I looped through all the values in my list, squared them, then added them to sum_squares.
In [6]:
sum_squares = 0
for i in lst:
sum_squares += i ** 2
I then created another new variable, sum_100, which would hold the sum of all numbers below 100, and set it to zero. Again, I looped through all the values in my list and added them to sum_100.
In [9]:
sum_100 = 0
for i in lst:
sum_100 += i
I then defined square_sum, which was the the square of the sum_100 variable. This is the square of the sum of all values up to 100.
In [11]:
square_sum = sum_100 ** 2
Finally, I created a variable, difference, and set it to the difference between square_sum and sum_squares. Printing difference displays the answer.
In [14]:
difference = square_sum - sum_squares
print(difference)
In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.