Creating Pythagorean triples with one leg <= some limit

Right now I am calculating the (primitive) Pythagorean paths this way

def generateTriples(limit):
    for n in xrange(1, limit):
        if (n**2 + (n+1)**2 > limit): 
            break
        for m in xrange(n+1, limit, 2):
            if (n**2 + m**2 > limit): 
                break
            if (gcd(n, m) > 1): 
                continue
            yield (m**2 - n**2, 2*m*n, m**2 + n**2)

But what it does is output all triples, where all legs fall less than or equal to the limit. For instance:

for triple in generateTriples(25):
    print triple
'''
(3, 4, 5)
(15, 8, 17)
(5, 12, 13)
(7, 24, 25)
'''

But what I would like to do is change it so that I limit only the legs. The hypotenuse can be as large as she wants - I just want the min (leg1, leg2) to be less than or equal to the limit.

I am also going to generate non-primitives, which means scaling by k in all terms (also such that min (leg1, leg2) is <= the limit, but I'm worried that I get duplicates this way.

Any advice would be highly appreciated.

+3
source share
1

, , :

from fractions import gcd

def generateTriples(limit):
    for n in xrange(1, limit):
        if (n**2 + (n+1)**2 > limit):
            break
        for m in xrange(n+1, limit, 2):
            if (n**2 + m**2 > limit):
                break
            if (gcd(n, m) > 1):
                continue
            yield (m**2 - n**2, 2*m*n, m**2 + n**2)

def generate_triples_limit_leg(leg):
    limit = leg**2 / 2 + 1

    for triple in generateTriples(limit):
        if min(triple) <= leg:
            yield triple

print list(generate_triples_limit_leg(i))

, :

def generate_triples(limit):
    # for each number up to the limit
    # Python ranges don't include the max number
    # start from 4 because 1 and 2 are invalid
    # and 3 and 4 give the same triplet
    for i in range(4, limit + 1):
        # if i is odd
        if i % 2:
            # the two larger legs are the integers
            # above and below half of the square of the smallest leg
            # // is floor division
            big = i**2 // 2
            yield i, big, big + 1
        else:
            # the two other legs are the integers
            # above and below half of the smallest leg squared
            big = (i // 2)**2
            yield i, big - 1, big + 1


print list(generate_triples(10))
# [(3, 4, 5), (5, 12, 13), (6, 8, 10),  (7, 24, 25), 
#             (8, 15, 17), (9, 40, 41), (10, 24, 26)]
+3

All Articles