The distribution of the trace of Frobenius and its applications in number theory