Home
Writeups Misc About
Legendre Symbol

Legendre Symbol

We are given the prime p and the integers to find the quadratic residue in p. The exact values of the prime is given in this link

Using Legendre Symbol and Euler's criterion, a number a can have three cases:

(ap)ap121 if a is a quadratic residue and a0modp
(ap)ap121 if a is a quadratic non-residue modp
(ap)ap120 if a0modp

But when the prime is of the form 4k+3, then using Euler's criterion, if a number a indeed has a quadratic residue, then:

ap121modp

Multiply both sides by a:

ap+12amodp

Denote the quadratic residue of a to be r, by definition of quadratic residue:

r2amodp

Combining the two equations above, we have:

r2ap+12modp

Taking the square root of both sides:

rap+14modp

Hence, the positive quadratic residue of r is given by ap+14modp

Otherwise, for the general case, we can use algorithms like Tonelli-Shanks and Cipolla's algorithm.

With this, we can easily solve the challenge by appending the following code to output.txt: