Advertisement
kingbode

Untitled

Sep 12th, 2023
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. """
  2.  
  3. A little mathematical challenge. Consider right-angle triangles with side lengths a, b and c ,
  4. where a is the shortest side and c the longest. Write a Python script to find all possible side lengths,
  5. where all three side lengths are integers. Don't include multiples of already found solutions.,
  6. 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.
  7.  
  8. For clarification, let's say that none of the sides can be longer than 1000.
  9. Rather than print all the combinations, tell us how many triangles you find.
  10. I would like to see solutions that don't import any libraries.
  11.  
  12. """
  13.  
  14. def searchForMultiples(_a,_b,_c,_triangles):
  15.     if len(_triangles) == 0:
  16.         return [[_a,_b,_c]] , False
  17.     for t in _triangles:
  18.         if _a % t[0] == 0:
  19.             divisor = _a / t[0]
  20.             if _b % t[1] == 0 and _b / t[1] == divisor:
  21.                 if _c % t[2] == 0 and _c / t[2] == divisor:
  22.                     return _triangles, True
  23.  
  24.     _triangles.append([_a,_b,_c])
  25.     print(_a, _b, _c)
  26.     return _triangles , False
  27.  
  28. def mainV1():
  29.     triangles = []
  30.     for a in range(1, 1001):
  31.         for b in range(a, 1001):
  32.             c = (a**2 + b**2)**0.5
  33.             if c.is_integer() and c <= 1000:
  34.                 c = int(c)
  35.                 triangles , checkForMultiples = searchForMultiples(a, b, c, triangles)
  36.  
  37.     print(f"Number of right-angle triangles found: {len(triangles)}")
  38.     return triangles
  39.  
  40. mainV1()
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement