title: Solving for x^2 + y^2 + z^2 = N to get all the unique combination of x, y, z
body: <p>The problem is I have to find all the possible combination of integers (x, y, z) that will satisfy the equation x^2 + y^2 + z^2 = N when you are given an integer N. You have to find all the unique tuples (x, y, z). For example, if one of the tuple is (1, 2, 1), then (2, 1, 1) is not unique anymore. </p>
  
  <pre><code>def find(n):
  
      ## max of x can be sqrt of n
      n1 = math.ceil(n ** (1/2))
  
      lst = []
  
      for i in range(1, n1):
  
          if (i ** 2) &gt;= n:
             break
  
          for j in range(1, (i + 1)):
             if (i ** 2 + j ** 2) &gt;= n:
                 break
             if (i ** 2 + i ** 2 + i ** 2) &lt; n:
                 break
  
             for k in range(1, (j + 1)):
                if (i ** 2 + j ** 2 + j ** 2) &lt; n:  
                   break
                if (i ** 2 + j ** 2 + k ** 2) &gt; n:       
                   break
                if i ** 2 + j ** 2 + k ** 2 == n:
                   a = [i, j, k]
                   lst.append(a)
      return lst          
  </code></pre>
  
  <p>I tried to make some optimizations. For example, I would stop going through the loop when it is not possible to satisfy the equation anymore. But it's still not optimized. I have a test case with a 8 digit integer, e.g 12345678. My code is taking quite a while to solve 8 digit integer. </p>
  
  <p>Is there any other optimization can be made? 
  Thank you!</p>