Python Implementation of Problem 6

View source code here on GitHub!

Problem Solution

Project Euler Problem 6

This turned out to be really easy

Problem:

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.

python.src.p0006.main() int
 1"""
 2Project Euler Problem 6
 3
 4This turned out to be really easy
 5
 6Problem:
 7
 8The sum of the squares of the first ten natural numbers is,
 91**2 + 2**2 + ... + 10**2 = 385
10
11The square of the sum of the first ten natural numbers is,
12(1 + 2 + ... + 10)**2 = 55**2 = 3025
13
14Hence the difference between the sum of the squares of the first ten natural
15numbers and the square of the sum is 3025 − 385 = 2640.
16
17Find the difference between the sum of the squares of the first one hundred
18natural numbers and the square of the sum.
19"""
20
21
22def main() -> int:
23    group = range(1, 101)
24    sum_of_squares = sum(x**2 for x in group)
25    square_of_sum = sum(group)**2
26    return square_of_sum - sum_of_squares

Tags: arithmetic-progression, sequence-summation