Skip to content

Instantly share code, notes, and snippets.

@yattom
Created December 8, 2014 00:05
Show Gist options
  • Save yattom/7d6c1644b3b7bbbcf03a to your computer and use it in GitHub Desktop.
Save yattom/7d6c1644b3b7bbbcf03a to your computer and use it in GitHub Desktop.
Search for right rectangles with edges of natural number length.
def right_triangle_of_natural_numbers(max_n):
'''
Return all right traiangles whose edges are natural number length.
Triangles of same ratio are discarded.
>>> right_triangle_of_natural_numbers(5)
[(5, 4, 3)]
>>> right_triangle_of_natural_numbers(20)
[(5, 4, 3), (13, 12, 5), (17, 15, 8)]
'''
t = []
for a in range(1,max_n + 1):
for b in range(1, a):
for c in range(1, b):
if a*a==b*b+c*c:
for n in range(1, a):
if (a/n, b/n, c/n) in t: break
else:
t.append((a, b, c))
return t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment