Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to count from 0 to n. And when I have reached n, adding 1 to n would give me 0 again.

For example:

0, 1, 2, 3, 4, 0, 1, 2, ...

Is there such an algorithm to do this?


I need an equation to simple get the number after the given number. In the example above if I put 1 in the equation I would get 2, if I put 4, I would get 0. Maybe algorithm isn't the best word for this problem.

share|cite|improve this question

closed as off topic by Ilmari Karonen, Rahul, J. M., Asaf Karagila, t.b. Feb 13 '12 at 16:41

Questions on Mathematics Stack Exchange are expected to relate to math within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Your question seems a little unclear. Obviously you're having no trouble counting up to $n$ and then starting from $0$ again, so what exactly do you need the algorithm for? – Ilmari Karonen Feb 11 '12 at 9:03
In this case, I would say that "algorithm" is another word for "piece of code". – utdiscant Feb 11 '12 at 9:06
@utdiscant: I think you're right, and that this question is simply on the wrong site; it belongs on SO, not here. – Ilmari Karonen Feb 11 '12 at 9:20
If you're looking for a function, you want $f(m):= $ the unique non-negative integer less than $n+1$ such that $m+1$ is congruent to $f(m) \operatorname{mod} n+1$. – Paul Slevin Feb 11 '12 at 10:06
up vote 0 down vote accepted

Here are a few ways to implement a function that adds one to $i$ and jumps back to $0$ after reaching $n$. I've chosen to write these in the C programming language, but they should be easy to port to other imperative languages.

This is probably the most straightforward way to accomplish the task:

int add_one_upto_n (int i, int n) {
    if (i >= n)
        return 0;
        return i + 1;

Alternatively, we can use the C divide-and-take-remainder operator %. This will work nicely on modern desktop CPUs, but may be inefficient on simple embedded systems where division is slow:

int add_one_upto_n (int i, int n) {
    return (i + 1) % (n + 1);

Also, if $n$ happens to equal $2^k-1$ for some $k$, the % operator may be replaced with a simple bitwise and operation:

int add_one_upto_n (int i, int n) {
    return (i + 1) & n;  /* only works if n + 1 is a power of 2 */

I think that about exhausts all the reasonable ways to do it I could think of. Of course, if we get into the realm of deliberately obfuscated code, I'm sure there are many more creative ways to accomplish this task...

share|cite|improve this answer

This will be the remainder when the consecutive whole numbers are divided by $n+1$.

A C++ code, if that please you:




cout<< k;


In the above code, Substitute your value of $n$. And, in between the place where there is a wild card entry, you could put in the maximum number upto which you'd like to count.

share|cite|improve this answer
A loop wouldn't be the best solution, because when I get the next number, I need to do other things with that number and the loop would eat my ram and cpu. An equation would be the best, but if it's not possible then I have recode all my application. – Cobold Feb 11 '12 at 9:03
What kind of formula do you need: I told you they are the remainder when the consecutive whole numbers are divided by $n+1$. So, the modulo % should do. If you tell me what exactly you need to do, "may be" I can be of some help. – user21436 Feb 11 '12 at 9:08

Maple counting code :


for a from 0  do

for k from 1 to n do
end do;

end do;

Output is sequence : $0,1,2,3,4,0,1,2,3,4,0,1....$

share|cite|improve this answer

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