Skip to content

Commit 2ef739f

Browse files
authored
Create Pythagorean_Triples_upto_an_Upper_Limit.py
1 parent f334b26 commit 2ef739f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Diff for: Pythagorean_Triples_upto_an_Upper_Limit.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Pythagorean Triples are quite unique in the mathematics of algebra and geometry (for aspiring high schoolers and professional mathematicians alike).
3+
Any triplets x, y, and z or tuples (x, y, z) from the set of natural numbers which satisfy the property that x² + y² = z² are called Pythagorean Triples.
4+
5+
Argument(s): None
6+
Returns: pythagorean triples (tuples of integers)
7+
8+
NOTE: This function only deals with integers. When floats are given, it may throw errors/exceptions.
9+
"""
10+
11+
# Defining the function to return all the Pythagorean Triples up until a certain upper limit.
12+
def PythagoreanTriples():
13+
limit = int(input("Enter the limit: "))
14+
while True:
15+
if (limit > 0):
16+
break
17+
else:
18+
print("Oh no! The number has to be positive.. Try again.")
19+
limit = int(input("Enter the limit: "))
20+
21+
c = 0
22+
m = 2
23+
24+
print("The Pythagorean triples upto %s are:"%str(limit))
25+
triples = []
26+
while(c < limit):
27+
for n in range(1, m):
28+
a = m**2 - n**2
29+
b = 2*m*n
30+
c = m**2 + n**2
31+
if (c > limit):
32+
break
33+
triples.append((a,b,c))
34+
print(a, b, c)
35+
m += 1
36+
return triples
37+
38+
if __name__ == "__main__":
39+
my_triples = PythagoreanTriples()
40+
print(f'There are {len(my_triples)} triples.')

0 commit comments

Comments
 (0)