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

Function solution

project_euler/problem_009/sol2.py:20–43  ·  view source on GitHub ↗

Return the product of a,b,c which are Pythagorean Triplet that satisfies the following: 1. a < b < c 2. a**2 + b**2 = c**2 3. a + b + c = n >>> solution(36) 1620 >>> solution(126) 66780

(n: int = 1000)

Source from the content-addressed store, hash-verified

18
19
20def solution(n: int = 1000) -> int:
21 """
22 Return the product of a,b,c which are Pythagorean Triplet that satisfies
23 the following:
24 1. a < b < c
25 2. a**2 + b**2 = c**2
26 3. a + b + c = n
27
28 >>> solution(36)
29 1620
30 >>> solution(126)
31 66780
32 """
33
34 product = -1
35 candidate = 0
36 for a in range(1, n // 3):
37 # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c
38 b = (n * n - 2 * a * n) // (2 * n - 2 * a)
39 c = n - a - b
40 if c * c == (a * a + b * b):
41 candidate = a * b * c
42 product = max(product, candidate)
43 return product
44
45
46if __name__ == "__main__":

Callers 1

sol2.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected