Skip to content

Commit f538ea5

Browse files
committed
Added General solution for Problem 9
1 parent 49a3396 commit f538ea5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Project Euler/Problem 9/sol2.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""A Pythagorean triplet is a set of three natural numbers, for which,
2+
a^2+b^2=c^2
3+
Given N, Check if there exists any Pythagorean triplet for which a+b+c=N
4+
Find maximum possible value of product of a,b,c among all such Pythagorean triplets, If there is no such Pythagorean triplet print -1."""
5+
#!/bin/python3
6+
import sys
7+
8+
product=-1
9+
d=0
10+
N = int(input())
11+
for a in range(1,N//3):
12+
"""Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c """
13+
b=(N*N-2*a*N)//(2*N-2*a)
14+
c=N-a-b
15+
if c*c==(a*a+b*b):
16+
d=(a*b*c)
17+
if d>=product:
18+
product=d
19+
print(product)

0 commit comments

Comments
 (0)