structural:reformatting, structural:scholar_edition
Modulo with offset

Sometimes it is useful for the result of a modulo n to lie not between 0 and n − 1, but between some number d and d + n − 1. In that case, d is called an offset and d = 1 is particularly common.

There does not seem to be a standard notation for this operation, so let us tentatively use a mod n. We thus have the following definition: x = a mod n just in case d ≤ x ≤ d + n − 1 and x mod n = a mod n. Clearly, the usual modulo operation corresponds to zero offset: a mod n = a mod n.

The operation of modulo with offset is related to the floor function as follows:

To see this, let x = a − n ⌊ a − d n ⌋ {\textstyle x=a-n\left\lfloor {\frac {a-d}{n}}\right\rfloor }. We first show that x mod n = a mod n. It is in general true that (a + bn) mod n = a mod n for all integers b; thus, this is true also in the particular case when b = − ⌊ a − d n ⌋ {\textstyle b=-\!\left\lfloor {\frac {a-d}{n}}\right\rfloor }; but that means that x mod n = (a − n ⌊ a − d n ⌋) mod n = a mod n {\textstyle x{\bmod {n}}=\left(a-n\left\lfloor {\frac {a-d}{n}}\right\rfloor \right)\!{\bmod {n}}=a{\bmod {n}}}, which is what we wanted to prove. It remains to be shown that d ≤ x ≤ d + n − 1. Let k and r be the integers such that a − d = kn + r with 0 ≤ r ≤ n − 1 (see Euclidean division). Then ⌊ a − d n ⌋ = k {\textstyle \left\lfloor {\frac {a-d}{n}}\right\rfloor =k}, thus x = a − n ⌊ a − d n ⌋ = a − n k = d + r {\textstyle x=a-n\left\lfloor {\frac {a-d}{n}}\right\rfloor =a-nk=d+r}. Now take 0 ≤ r ≤ n − 1 and add d to both sides, obtaining d ≤ d + r ≤ d + n − 1. But we've seen that x = d + r, so we are done.

The modulo with offset a mod n is implemented in Mathematica as Mod.