Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- A little mathematical challenge. Consider right-angle triangles with side lengths a, b and c ,
- where a is the shortest side and c the longest. Write a Python script to find all possible side lengths,
- where all three side lengths are integers. Don't include multiples of already found solutions.,
- for example, your result should show 3, 4, 5 but not 6, 8, 10. Compute for all integer lengths of a from 1 to 1000.
- For clarification, let's say that none of the sides can be longer than 1000.
- Rather than print all the combinations, tell us how many triangles you find.
- I would like to see solutions that don't import any libraries.
- """
- def searchForMultiples(_a,_b,_c,_triangles):
- if len(_triangles) == 0:
- return [[_a,_b,_c]] , False
- for t in _triangles:
- if _a % t[0] == 0:
- divisor = _a / t[0]
- if _b % t[1] == 0 and _b / t[1] == divisor:
- if _c % t[2] == 0 and _c / t[2] == divisor:
- return _triangles, True
- _triangles.append([_a,_b,_c])
- print(_a, _b, _c)
- return _triangles , False
- def mainV1():
- triangles = []
- for a in range(1, 1001):
- for b in range(a, 1001):
- c = (a**2 + b**2)**0.5
- if c.is_integer() and c <= 1000:
- c = int(c)
- triangles , checkForMultiples = searchForMultiples(a, b, c, triangles)
- print(f"Number of right-angle triangles found: {len(triangles)}")
- return triangles
- mainV1()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement