1
$\begingroup$

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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.