TheAlgorithms-Python
43 строки · 1.0 Кб
1"""
2Project Euler Problem 6: https://projecteuler.net/problem=6
3
4Sum square difference
5
6The sum of the squares of the first ten natural numbers is,
71^2 + 2^2 + ... + 10^2 = 385
8
9The square of the sum of the first ten natural numbers is,
10(1 + 2 + ... + 10)^2 = 55^2 = 3025
11
12Hence the difference between the sum of the squares of the first ten
13natural numbers and the square of the sum is 3025 - 385 = 2640.
14
15Find the difference between the sum of the squares of the first one
16hundred natural numbers and the square of the sum.
17"""
18
19import math20
21
22def solution(n: int = 100) -> int:23"""24Returns the difference between the sum of the squares of the first n
25natural numbers and the square of the sum.
26
27>>> solution(10)
282640
29>>> solution(15)
3013160
31>>> solution(20)
3241230
33>>> solution(50)
341582700
35"""
36
37sum_of_squares = sum(i * i for i in range(1, n + 1))38square_of_sum = int(math.pow(sum(range(1, n + 1)), 2))39return square_of_sum - sum_of_squares40
41
42if __name__ == "__main__":43print(f"{solution() = }")44