This formula only works if it is known in advance that is a quadratic residue, which can be checked using the law of quadratic reciprocity.
This formula only works if it is known in advance that is a quadratic residue, which can be checked using the law of quadratic reciprocity.