Project Euler, problem # 9, Pythagorean triplet Project Euler #9, Pythagorean triplet is


  A Pythagorean triplet is a set of three natural numbers \$a &lt; b &lt; c\$
  for which \$a^2 + b^2 = c^2\$.
  
  For example, \$3^2 + 4^2 = 9 + 16 = 25 = 5^2\$.
  
  There exists exactly one Pythagorean triplet for which \$a + b + c = 1000\$.   Find the product \$a b c\$.


Here is my implementation in Python, awaiting your feedback.

def get_triplet():
    for c in range(2, 1000):
        for b in range(2, c):
            a = 1000 - c - b
            if a ** 2 + b ** 2 == c ** 2:
                return 'n1 = %s\nn2 = ' \
                       '%s\nn3 = %s\nproduct = %s' \
                       % (a, b, c, a * b * c)


if __name__ == '__main__':
    print(get_triplet())
Search Space

Your search space is too large.  Since 0 &lt; a &lt; b &lt; c and a + b + c = 1000, you can put hard limits on both a and c.  The maximum a can be is 332, since 333 + 334 + 335 &gt; 1000.  Similarly, the minimum value c can be is 335, since 332 + 333 + 334 &lt; 1000.  As well, the maximum value c can have is 997 (1 + 2 + 997 = 1000).

Based on the above, if you wish your outside loop to loop over c values, your first loop should be:

for c in range(335, 997 + 1):
    # ...


And the inner loop should be over a values:

    for a in range(1, 332 + 1):
        b = 1000 - c - a
        # ...


But now that we’ve selected a c value, can tighten the limits on a even further.  When c is 600, then a+b must be 400, and since a &lt; b must hold, the maximum a can have is 400 / 2.  So:

    for a in range(1, min(332, (1000 - c) // 2) + 1):
        b = 1000 - c - a
        # ...


Moreover, since we’ve selected a c value, we have established a maximum value for b as well, since b &lt; c must hold.  If c is 400, then b can’t be larger than 399, so a must be at least 1000 - 400 - 399 = 201, establishing a lower limit for our inner loop:

    for a in range(max(1, 1000 - c - (c - 1)), min(332, (1000 - c) // 2) + 1):
        b = 1000 - c - a
        # ...


Geometry

As pointed out in a comment by @Peter Taylor below, we can tighten the limits on c much further.

First, triangles must be triangular.  In every triangle, the length of any side must be less than the sum of the lengths of the other two sides.  In particular, \$c &lt; a + b\$ must be true.  Since \$a + b + c = 1000\$, we can conclude \$c &lt; 1000 - c\$, or more simply, \$c &lt; 500\$, establishing a much smaller upper limit.

The smallest value c could obtain would be when a and b are at their largest.  If we relax the conditions slightly, allowing \$a \leq b\$ and \$a, b, c, \in \Re\$; we have an isosceles right-angle triangle, \$a = b = c / \sqrt 2\$.  Then:

\$a + b + c = 1000\$

\$c = 1000 \sqrt 2 / (2 + \sqrt 2) \approx 414.2\$

giving us a larger lower limit.

Thus, the range of the outer loop can be reduced to:

for c in range(413, 500):
    # ...


Repeated Calculations

When searching for the Pythagorean triplet, you are calculating c ** 2 each and every iteration of the inner loop.  The value of c is constant during execution of the inner loop, so c ** 2 is also constant.  Since exponentiation is a relatively expensive operation, you should get a speed improvement by moving this calculation out of the inner loop:

for c in range(413, 500):
    c2 = c ** 2
    for a in range(max(1, 1000 - c - (c - 1)), min(332, (1000 - c) // 2) + 1):
        b = 1000 - c - a
        if a ** 2 + b ** 2 == c2:
            return a, b, c


1000 - c is also repeated in the inner loop, so you could compute this outside the loop also; it should provide a slight performance improvement.



See other answers for important PEP8, f-string, docstring, and return value improvements not duplicated here.