$o_n = 2n + 1$
$e_n = (2n)^2$
$r_i = \mathop{\min} \{ n \mid e_n \ge i\}$
$a_n = n + e_n$
$q_i = \left\lfloor \frac{i - a_{r_i}}{o_{r_i}} \right\rfloor \mod 4$
Is there another way to write $q_i$ without having to compute an $\min$?
$r_i$ can be rewritten as $r_i = \left\lceil \frac {\sqrt i} 2 \right\rceil$
This removes the need to compute the minimum.