# Efficient indexing when index must cycle

I've defined a function indexed through a cycle:

\$Assumptions =
n > 3 && n \[Element] Integers && i \[Element] Integers && i <= n &&
i >= 1
f[i_, n_] := c[Mod[i + 1, n, 1]] + c[Mod[i - 1, n, 1]] + c[Mod[i - 1, n, 1]] + c[Mod[i + 2, n, 1]] + c[Mod[i - 2, n, 1]]


Once the index $$i$$ reaches $$n$$, $$i+1$$ returns $$1$$, $$i+2$$ returns $$2$$ etc. Is there a more efficient way of defining it?

• The Mod[i, n, 1] construct is pretty much the best way to do what you want. – J. M.'s ennui Apr 18 '20 at 10:28
• slightly shorter/cleaner way: g[i_, n_] :=Total[c /@ Mod[Range[i - 2, i + 2], n, 1]]? – kglr Apr 18 '20 at 10:55