5

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 }

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
maddingl
  • 153
  • 2

1 Answers1

13

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.

Sjoerd Smit
  • 23,370
  • 46
  • 75