# ptwocheck.py -- exact P_2(N,q) (probability that N independent uniform q-th roots of unity sum to 0), by dynamic # programming on the integer coordinates in the power basis of Z[w], against the asymptotic formula used in section 1: # P_2(N,q) ~ h (2 pi N)^(-s/2) (q^s / prod_{p|q} p^(s/(p-1)))^(1/2), s = phi(q), h = Phi_q(1). import math from latticecheck import cyclo, reduce_mod from fractions import Fraction def primes(q): return [p for p in range(2,q+1) if q%p==0 and all(p%r for r in range(2,p))] def exact(q,N): P=cyclo(q); s=len(P)-1; steps=[tuple(reduce_mod(e,P)) for e in range(q)] dist={tuple([0]*s):1} for _ in range(N): new={} for v,c in dist.items(): for st in steps: w=tuple(a+b for a,b in zip(v,st)); new[w]=new.get(w,0)+c dist=new return dist.get(tuple([0]*s),0)/q**N def formula(q,N): P=cyclo(q); s=len(P)-1; h=sum(P) return h*(2*math.pi*N)**(-s/2)*math.sqrt(q**s/math.prod(p**(s/(p-1)) for p in primes(q))) if __name__=='__main__': for q,Ns in ((4,(40,80,160)),(6,(30,60,120)),(8,(16,24,32)),(12,(12,18,24)),(9,(18,))): for N in Ns: e=exact(q,N); print(f"q = {q:2d}, N = {N:3d}: exact {e:.6e}, formula {formula(q,N):.6e}, ratio {e/formula(q,N):.4f}",flush=True)