= Solution
Before testing candidate $i$, every earlier integer has been printed exactly when it belongs to the spectrum, and every positive $h<k$ satisfies $rh^2<i^2$. During the inner loop that exclusion remains true: an excluded candidate is passed over only after checking its square.
At termination of the inner loop, $rk^2\geq i^2$, so $k$ is the least positive possible witness. If its square is already at least $(i+1)^2$, every larger witness is too large as well; otherwise it satisfies both required inequalities and $i$ is printed. Advancing $i$ preserves the earlier-witness exclusion because the new lower bound only increases. These <loop invariants> establish both completeness and absence of extra outputs.
Back to article page