How to shift all numbers in a cycle?

My son had an interesting math puzzle, and I couldn’t come up with a solution, so I thought I’d ask here.

Let’s say you have a cycle of the numbers from 1 to 12 - like the hours on a clock. After 12 comes 1 again.

Is there any type of formula, using simple mathematical functions (like add, multiply, divide, exponents, etc) to move each number up by 6? So 1 would become 7, 3 would become 9, 6 would become 12, and 9 would become 3?


It sounds like you want modulo - that is, the remainder after a number is divided by another number. In the clock example, it would be modulo 12, and you have to add a little fudge factor to make up for the fact that it’s going from 1 to 12, not 0 to 11

Adding 6 (using ordinary mod) would go
(1+6) mod 12 = 7
(2+6) mod 12 = 8

(9+6) mod 12 = 3
(10+6) mod 12 = 4

…and so on and so on.

There is an edge case at 6 itself - (6+6) mod 12 = 0, where you want 12. So the pedantically correct way of adding 6 to some clock hour x is

(x+5) mod 12 +1

But it’s easier to see the general principle using the simple version first

Just add 6, and then subtract 12 from any number greater than 12. If you have an arbitrary large number and need to cast out multiples of 12, you should divide by 12 as usual but keep only the remainder. (E.g., 15 = 12⋅1 + 3)

Thanks - he reports that the solution at the end is what he was looking for. He had come up with your first answer, but 6 becoming 0 was a problem.

It’s weird - I thought I was pretty good in high school math (30 years ago!) but I don’t remember anything about mod

It’s pretty useful in programming (and statistics, often). In a purely maths context though, you kinda learn it and forget it - unless you’re getting into Group Theory, which is pretty esoteric

Also note how the order of operations can be swapped when doing addition and subtraction with modulo.

If you routinely want to shift things by, say, 47264857, you don’t need to add that to your hour and then take the mod. You can take the mod of 47264857 first – which is 1, since that number is one more than a multiple of 12 (i.e., 3938738 x 12 + 1 = 47264857) – and then just add that result. The first 3938738 cycles end up doing nothing, so you can leave them out from the start.

So, shifting any number by 47264857 (mod 12) is no different than shifting by 1 (mod 12).

This could somehow matter if you are doing certain calculations. For example, suppose you are calculating a function of an angle, like sin 2α. Suppose α is given in degrees. If α = 47264857, you may be facing a loss of precision or other problem when you try this, whereas if you, or your compiler, reduces modulo 360.0 first, you will be in good shape.

Alternatively, if you have 1 to N shifted by k, the value x will result in

x+k if x<=N-k
x-(N-k) if x>N-k

That would only work for a narrow range of k values. If k=25, say, your algorithm would shift x=1 to 14.

This is such a basic and useful operation that most computer languages have it built in. For example, in the languages descended from the C programming language (for example, C, C++, C#, and Java) 10 mod 3 would be written as 10 % 3.

One subtlety is that % is actually the remainder operator, which only makes a difference for negative numbers, which can give a negative remainder, while the modulus is always positive.