We can use the techniques described here: http://duriansoftware.com/joe/Optimizing-is-multiple-checks-with-modular-arithmetic.html