What you want to do is pretty much the definition of "modulo arithmetic." If the built-in arithmetic in your database cannot perform the calculations quickly enough, I'm not sure what you could do to improve this without creating your own database engine or buying more powerful hardware.
Is the problem really arithmetic speed, or is it recording or indexing a record? I mean, if you say something like:
select ... whatever ... from table1 join table2 using (someid)
where (table1.q / table2.d) % 10 = 4
(table1.q/table2.d)% 10, , db , , , , .