5
$\begingroup$

Is there a way to to express the following without using an if statement, but with mathematical operations instead?

{ 5 if x % 5 = 0, x % 5 otherwise }

$\endgroup$
1
  • 2
    $\begingroup$ Mod[x-1,5]+1? $\endgroup$
    – mikado
    Oct 28, 2019 at 23:19

1 Answer 1

13
$\begingroup$

You can use the 3rd offset argument of Mod for this:

Mod[x, 5, 1]

The 3rd argument effectively shifts the point where Mod loops back. So Mod[x, 5, 0] loops from 0 to 4 and Mod[x, 5, 1] loops from 1 to 5.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.