import numberdb.sage as numberdb
from sage.arith.misc import kronecker_symbol
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
def legendre_merit_factor(p):
u = [ZZ(1)] + [ZZ(kronecker_symbol(j, p)) for j in range(1, p)]
denominator = ZZ(0)
for k in range(1, p):
c = sum(u[j] * u[j + k] for j in range(p - k))
denominator += c * c
return QQ(p * p) / QQ(2 * denominator)
legendre_merit_factor(1009) # the next prime after this tableThe generator computes the Legendre sequence from exact Legendre symbols and forms the aperiodic autocorrelations by integer arithmetic. The values are exact rational numbers.
The run checked every value against the same sequence built by Euler's criterion rather than Sage's Legendre symbol, against the known small values for $p=3,5,7,11,13$, and against the $L^4$-norm formula computed from all ordered coefficient pairs.