Efficiently find all the Pythagorean triplets where all numbers less than 1000 How can I make this code run faster:
for a in range(1,1001):
    for b in range(1, 1001):
        for c in range(1, 1001):
            if pow(a, 2) + pow(b, 2) == pow(c, 2):
                print(str(a) + &quot;,&quot; + str(b) + &quot;,&quot; + str(c))
Some optimizations and style suggestions:

After finding a solution you can break:
for a in range(1,1001):
    for b in range(1, 1001):
        for c in range(1, 1001):
            if pow(a, 2) + pow(b, 2) == pow(c, 2):
                print(str(a) + &quot;,&quot; + str(b) + &quot;,&quot; + str(c))
                break


Use ** which is faster than pow, or just multiply for itself a*a.
Use Python formatter to print the result: print(f&quot;{a},{b},{c}&quot;).
Calculate c as \$c=sqrt(a^2+b^2)\$:
for a in range(1,1001):
    for b in range(1, 1001):
        c = int(math.sqrt(a ** 2 + b ** 2))
        if a ** 2 + b ** 2 == c ** 2 and c &lt; 1001:
            print(f&quot;{a},{b},{c}&quot;)

The solution now takes \$O(n^2)\$ instead of \$O(n^3)\$.
Instead of checking if a ** 2 + b ** 2 == c ** 2:, it's enough verifying that c is an integer:
for a in range(1,1001):
  for b in range(1, 1001):
      c = math.sqrt(a ** 2 + b ** 2)
      if c.is_integer() and c &lt; 1001:
          print(f&quot;{a},{b},{int(c)}&quot;)


As already said, you can also start the second for-loop from a to avoid duplicated solutions.
Put everything into a function:
def triplets(n):
  for a in range(1, n):
      for b in range(a, n):
          c = math.sqrt(a * a + b * b)
          if c.is_integer() and c &lt;= n:
              print(f&quot;{a},{b},{int(c)}&quot;)
triplets(1000)



Runtime on my machine:
Original: 868.27 seconds (~15 minutes)
Improved: 0.27 seconds


EDIT:
Since this question got a lot of attention I wanted to add a couple of notes:

This early answer was for OP's original problem that I interpreted as &quot;find all the triplets in a reasonable amount of time&quot;.
There are definitely more efficient (and advanced) solutions than this one. If you are interested in finding out more, have a look at other excellent answers on this thread.
As noted in the comments, the runtime in my answer is a rough calculation. Find a better benchmark on @Stefan's answer.