I am trying a number as the sum of 3 squares.  For my particular project I will require a complete list since the next step is to compute nearest neighbors (here).In any case we'd like to find \$ M = x^2 + y^2 + z^2 \$ in all possible ways.  Here's what I wrote in Python:M = 1062437m = int(np.sqrt(M))x = np.arange(int(np.sqrt(m)))y = M - x**2sq = [(i,j, int(np.sqrt(M - i**2 - j**2))) for i in range(m) for j in range(m) if i**2 + j**2 in y]This is easy to explain in pseudocode.build a list of remainders \$\{ M - k^2 : 0 \leq k \leq \sqrt{M} \}\$loop over all possible \$0 < i,j < \sqrt{M} \$ check that \$i^2 + j^2 \$ is in the listThis is one of several possible strategies.  In my case, I don't even need a fixed M it could be a range, such as 10**6 < M < 10**6 + 10*4.I do not  mind looping over values of M at this point. Can we recycle our computations somehow?
Listing the representations of integer M as a sum of three squares M = x^2 + y^2 + z^2
python;array;combinatorics;integer