For wrapping some sum x + y inside of a range n, you can use (x + y) % n (and of course bounds-checking with if works too).
x + y
n
(x + y) % n
if
N.B. different languages treat negative mod differently. For JS, in the negative case you'd want something like ((x % n) + n) % n instead of plain %.
((x % n) + n) % n
%
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
For wrapping some sum
x + y
inside of a rangen
, you can use(x + y) % n
(and of course bounds-checking withif
works too).N.B. different languages treat negative mod differently. For JS, in the negative case you'd want something like
((x % n) + n) % n
instead of plain%
.