""" Demonstrates use of list comprehension, range() and math.hypot(). This is a brute force algorithm and is practical only for smallish n. Much better algorithms are out there. a,b can never be same length if c is to be an integer. Since (3,4,5) and (4,3,5) are the same triple, we force a < b to preserve uniqueness, i.e. find (a,b,c) where a < b < c. The gcd check excludes similar triangles e.g. if (3,4,5) has been found, we don't also want (6,8,10). """ from math import hypot def triples(n): """ Extract Pythagorean triples up to longer leg b = n """ checklist = [(a,b,hypot(a,b)) for b in range(1,n+1) for a in range(1,b) if gcd(a,b)==1] return [(a,b,int(c)) for (a,b,c) in checklist if eqint(c)] def gcd(a,b): """ Find greatest common divisor of a,b """ while b: a,b = b, a%b return a def eqint(a): """ Determine whether a is an integer """ if int(a)==a: return 1 else: return 0