Project Euler Solution 6: Sum square difference
This is part of the Project Euler series, this is about Problem 6: Sum square difference. It is about finding the difference between two series.
The sum of the squares of the first ten natural numbers is, $$ 1^2 + 2^2 + \cdots + 10^2 = 385 \,. $$
The square of the sum of the first ten natural numbers is, $$ (1 + 2 + \cdots + 10)^2 = 55^2 = 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.
I don't see the need of a trick here. Just do a straightforward computation and one is done:
def solution_naive() -> int: sum_numbers = 0 sum_squares = 0 for i in range(101): sum_numbers += i sum_squares += i**2 return sum_numbers**2 - sum_squares
The solution is 25,164,150 and it took 7 µs to compute it. The number also isn't so large that it would give an integer overflow with a 32 bit integer. So this problem seems to be easier than the problems before.