Is there an algorithm to find integers such that the following holds true : ${a_1}^2 + {a_2}^2 \dots + {a_k}^2 \equiv p $ $(mod $ $n)$, where $p$ is a prime and $n$ is any general integer.
By Lagrange 4 square theorem it is known that value of $k$ will be atmost 4. Also according to this post since, we are working in modular fields, the value of $k$ will be atmost 2, i.e. when it is not a quadratic residue. But I was interested in finding the integers rather than value of $k$. Please suggest an efficient algorithm for the above.