Algorithm Training/5%

#6 Sum square difference

Hwigang Lee 2022. 2. 1. 23:00

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 = 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.


더보기

The square of a sum is equal to the sum of the squares of all the summands plus the sum of all the double products of the summands in twos.

\[\left( \sum_{i}x_i \right)^2=\sum_{i}{x_i}^2 + 2\sum_{i<j}x_i x_j\]

\[\left ( \sum_{i} x_i \right )^2 - \sum_{i} {x_i}^2 = 2 \sum_{i<j} x_i x_j\]

n = 100
summation = 0
for j in range(1, n + 1):
    for i in range(1, j):
        summation += 2 * i * j

The answer is 25164150.

'Algorithm Training > 5%' 카테고리의 다른 글

#9 Special Pythagorean triplet  (0) 2022.02.04
#8 Largest product in a series  (0) 2022.02.03
#3 Largest prime factor  (0) 2022.02.01
#5 Smallest multiple  (0) 2022.01.30
#4 Largest palindrome product  (0) 2022.01.28