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$
  • 2
    $\begingroup$ Mod[x-1,5]+1? $\endgroup$ – mikado Oct 28 '19 at 23:19
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.

| improve this answer | |
$\endgroup$

Your Answer

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

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