def triplets_with_sum(n):
    pass

@@ -1,35 +1,1260 @@
-def triplets_with_sum(n):%0A    pass
+from math import ceil, gcd, sqrt%0A%0A%0Adef triplets_in_range(range_start, range_end):%0A    for limit in range(range_start, range_end, 4):%0A        for x, y, z in primitive_triplets(limit):%0A            a, b, c = (x, y, z)%0A%0A            # yield multiples of primitive triplet%0A            while a %3C range_start:%0A                a, b, c = (a + x, b + y, c + z)%0A            while c %3C range_end:%0A                yield (a, b, c)%0A                a, b, c = (a + x, b + y, c + z)%0A%0A%0Adef euclidian_coprimes(limit):%0A    %22%22%22See Euclidean algorithm%0A    https://en.wikipedia.org/wiki/Euclidean_algorithm#Description%0A    %22%22%22%0A    mn = limit // 2%0A    for n in range(1, int(ceil(sqrt(mn)))):%0A        if mn %25 n == 0:%0A            m = mn // n%0A            if (m - n) %25 2 == 1 and gcd(m, n) == 1:%0A                yield m, n%0A%0Adef primitive_triplets(limit):%0A    %22%22%22See Euclid's formula%0A    https://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple%0A    %22%22%22%0A    for m, n in euclidian_coprimes(limit):%0A        a = m ** 2 - n ** 2%0A        b = 2 * m * n%0A        c = m ** 2 + n ** 2%0A        yield sorted(%5Ba, b, c%5D)%0A%0A%0Adef triplets_with_sum(triplet_sum):%0A    return %7B%0A        triplet%0A        for triplet in triplets_in_range(1, triplet_sum // 2)%0A        if sum(triplet) == triplet_sum%0A    %7D
 %0A

Solve pythagorean_triple - reference solution