TheAlgorithms-Python

Форк
0
43 строки · 1.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
import math
20

21

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

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

37
    sum_of_squares = sum(i * i for i in range(1, n + 1))
38
    square_of_sum = int(math.pow(sum(range(1, n + 1)), 2))
39
    return square_of_sum - sum_of_squares
40

41

42
if __name__ == "__main__":
43
    print(f"{solution() = }")
44

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

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

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

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