Creating modulo formula to convert numbers

171 Views Asked by At

I need a formula that makes the following conversion from a to b:

a    b
0 -> 2
1 -> 3
2 -> 4
3 -> 5
4 -> 6
5 -> 7
6 -> 1

By trial and error, I came up with the the following formula: $$b = (a + 1) \,\text{mod } 7 + 1$$

How to come up with the equation in a systematic manner?

Background: The problem originates from converting weekdays from zero-indexed (with Monday first) system to Java Calendar's one-indexed (with Sunday first).

1

There are 1 best solutions below

0
On BEST ANSWER

First, note that $a$ ranges from $0$ to $6$, inclusive, so there must be a `modulo $7$' sort of thing involved. Also, $b$ ranges from $1$ to $7$, which is the same as $a$'s range, but with a $1$ added. Subtracting this extra $1$, we observe that $b$ is still one greater than $a$, modulo $7$, and hence the formula.