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

Function solution

project_euler/problem_006/sol1.py:20–40  ·  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

18
19
20def 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_of_squares = 0
36 sum_of_ints = 0
37 for i in range(1, n + 1):
38 sum_of_squares += i**2
39 sum_of_ints += i
40 return sum_of_ints**2 - sum_of_squares
41
42
43if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected