TheAlgorithms-Python

Форк
0
41 строка · 1021.0 Байт
1
"""
2
Project Euler Problem 6: https://projecteuler.net/problem=6
3

4
Sum square difference
5

6
The sum of the squares of the first ten natural numbers is,
7
    1^2 + 2^2 + ... + 10^2 = 385
8

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

12
Hence the difference between the sum of the squares of the first ten
13
natural numbers and the square of the sum is 3025 - 385 = 2640.
14

15
Find the difference between the sum of the squares of the first one
16
hundred natural numbers and the square of the sum.
17
"""
18

19

20
def solution(n: int = 100) -> int:
21
    """
22
    Returns the difference between the sum of the squares of the first n
23
    natural numbers and the square of the sum.
24

25
    >>> solution(10)
26
    2640
27
    >>> solution(15)
28
    13160
29
    >>> solution(20)
30
    41230
31
    >>> solution(50)
32
    1582700
33
    """
34

35
    sum_cubes = (n * (n + 1) // 2) ** 2
36
    sum_squares = n * (n + 1) * (2 * n + 1) // 6
37
    return sum_cubes - sum_squares
38

39

40
if __name__ == "__main__":
41
    print(f"{solution() = }")
42

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.