Listing integers as the sum of three squares $m = x^2 + y^2 + z^2$
Your first approach is probably the simplest to implement.  Its running time will be $O(m)$.

Another approach: you could loop over all $x$ such that $0 &lt; x &lt; \sqrt{m}$, and for each $x$ enumerate all ways to express $m-x^2$ as a sum of two squares.  There's a lot of theory about how to express an integer as a sum of two squares (e.g., Lagrange's algorithm).  However, this will be more complex to implement.