For which values of i is (yi2 − n) divisible by pj? Define a = floor(sqrt(n)), a constant.
(yi2 ≠ n) = (a+i)2 − n.
Given pj, we need to know for which values of i does pj divide (a+i)2 − n.
For half of the primes there will be no solutions and for the other half there will be two solutions per period of size pj.
Solving this equation involves taking square root modulo some small prime, not a particularly difficult thing to do.
If i solves the equation then so does i+pj.
A simple loop thus augments the counters for all those i’s where pj divides (yi2 − n).