MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_006/sol3.py:22–39  ·  view source on GitHub ↗

Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. >>> solution(10) 2640 >>> solution(15) 13160 >>> solution(20) 41230 >>> solution(50) 1582700

(n: int = 100)

Source from the content-addressed store, hash-verified

20
21
22def 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
42if __name__ == "__main__":

Callers 1

sol3.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected