Enzo, here is some relevant maths to what you have posted here:
The number 51, in your case, you were testing to see whether it was a quadratic residue (mod 163), that is, whether or not there is a square that is 51 (mod 163). There is a wellknown theorem that states that if a number b is a quadratic residue (mod N), then there are infinitely many squares that are b (mod N). In this case, you found that for k=10, there is a square of the form k*N+b for N=163, b=51, therefore 51 is a quadratic residue (mod 163), and there are infinitely many squares of this form using the above theorem.
