Lsym <- Vectorize(legendre_sym, 'a')
# all quadratic residues of p = 17
qr17 <- which(Lsym(1:16, 17) == 1) # 1 2 4 8 9 13 15 16
sort(unique((1:16)^2 %% 17)) # the same
if (FALSE) {
# how about large numbers?
p <- 1198112137 # isPrime(p) TRUE
x <- 4652356
a <- mod(x^2, p) # 520595831
legendre_sym(a, p) # 1
legendre_sym(a+1, p) # -1
}
jacobi_sym(11, 12) # -1
Run the code above in your browser using DataLab