posted 1 year ago
It's not really a trick. The first thing to observe is that you want the numbers 1, 2, 3, ..., 26 to repeat. So you need "% 26" to make that happen (and not "% 25" and not "% 27"). But that produces 0, 1, 2, ..., 25 repeating. So you need to add 1 at the end to fix that. But then you find the results starting at 2 instead of 1. So you need to subtract 1 at the beginning to fix that. This results in your formula.
I've found that to devise this sort of transformation it helps to write things in columns and adjust things as necessary. Like so:
Input i%26
1 1
2 2
...
25 25
26 0
But we don't want the 0 in that row, we want 26 there. But you can't get 26 as the result of %26 so you have to add 1 at the end:
Input i%26+1
1 2
2 3
...
25 26
26 1
But now the results are offset so you have to subtract 1 at the beginning:
Input (i-1)%26+1
1 1
2 2
...
25 25
26 26
It looks easy the way I did it but in practice it takes a bit of futzing around before you get it right.