I have two integers: n
and m
:
-
n
: Number that will repeat in the index; -
m
: Index maximum.
What I need to do is simple, is to return a number that is <= m
based on n
. When n
is greater, it will repeat how many times it "went out" of the maximum, for example:
10 repete 10 = 10
5 repete 10 = 5
15 repete 10 = 5
30 repete 10 = 10
11 repete 10 = 0
The minimum is 0 .
I set up this account in C # to return according to these values, but I was not successful:
static int Rotate(int n, int m) {
if (n <= m) return n;
int d = n / m;
n -= d * m;
return n - 1;
}
And the output of the above code, using the same repeated expressions in the above example were respectively:
10 -- correto
5 -- correto
4 -- errado
-1 -- errado
0 -- correto
In other words, when n
is greater than m
, it returns to index 0
and goes to whatever is left, repeating if it is greater than m
:
15 repete 10:
m = 0 1 2 3 4 5 6 7 8 9 10 0 1 2 4 5
n = 1 2 3 4 5 5 6 7 8 9 10 11 12 13 14 15
resultado = ^^
How do I create a function to repeat these numbers at this maximum rate?