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

Function solution

project_euler/problem_009/sol4.py:36–56  ·  view source on GitHub ↗

Precomputing squares and checking if a^2 + b^2 is the square by set look-up. >>> solution(12) 60 >>> solution(36) 1620

(n: int = 1000)

Source from the content-addressed store, hash-verified

34
35
36def solution(n: int = 1000) -> int:
37 """
38 Precomputing squares and checking if a^2 + b^2 is the square by set look-up.
39
40 >>> solution(12)
41 60
42 >>> solution(36)
43 1620
44 """
45
46 squares = get_squares(n)
47 squares_set = set(squares)
48 for a in range(1, n // 3):
49 for b in range(a + 1, (n - a) // 2 + 1):
50 if (
51 squares[a] + squares[b] in squares_set
52 and squares[n - a - b] == squares[a] + squares[b]
53 ):
54 return a * b * (n - a - b)
55
56 return -1
57
58
59if __name__ == "__main__":

Callers 1

sol4.pyFile · 0.70

Calls 1

get_squaresFunction · 0.85

Tested by

no test coverage detected