Problem 6

The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Solution 1: Maths

To get sum of squares we just need to calculate for primes below 100. For (2, 4, 8, 16, ...), (3, 9, 24, ...), ... are progression with pattern. right? so we use mathematical equation.

Here is the result of progression of all primes.

To get square of sum, we just find arithmetic series and square it.

Solution 2: Brute Forcing

Brute forcing is enough here.

def square_sum(n):
    return sum(map(lambda x: x**2, list(range(1,n+1))))


def sum_square(n): return sum(list(range(1,n+1)))**2

print(sum_square(100)- square_sum(100))

The code run: ~0.0s.
The output: 25164150

.