How can I determine if a number is a part of arithmetic sequence

8.5k Views Asked by At

Sorry if this is a simple question, I am not so good in mathematics and trying to start understanding it better. So thanks in advance.

I have this sequence - $0,1,5,6,10,11,15,16,20,21$.... Which is $n$ numbers and then a gap of $n+1$. What I need is a way to get a random number and check if it is part of the sequence.

I see that I can check the last number and see if it is $(0,1,5,6)$ but this will be true only for this sequence. So I need more generic way.

Thanks Shani

edit The sequence starts from $0$ $n = 2$ Then we start with $n$ numbers $(0,1)$ skip $n+1$ numbers $(2,3,4)$ then include $n$ numbers $(5,6)$ , skip $n+1$ $(7,8,9)...$

2

There are 2 best solutions below

0
On

Okay generally speaking you need to have a formula for the terms of the sequence. In your case the formula seems to be $$a_{2n} = 5n\; , \; a_{2n+1} = 5n+1$$ However it might as well be a completely different formula, this is completely possible!

The basic notion is to try and find the relations between the terms and express them properly.

0
On

Your description of your sequence is perhaps not very practical when it comes to calculations. But the procedure for producing elements of the sequence is a periodic one. You take an input sequence of natural numbers and pick two and then skips $n$ and repeat. This means that you consume $n+2$ integers from the input sequence for each round through the algorithm.

You can reformulate the produce by working in chunks. You take a chunk of $n+2$ integers and emit the first two.

Now it shouldn't be too hard to see that the $(j+1)$th chunk consist of the numbers $(n+2)j, (n+2)j+1, \dots, (n+2)j+n+1$ of which we pick the first two numbers. This results in that we see that the numbers we produce is $(n+2)j$ and $(n+2)j+1$.

So the question is transformed to the question whether a random number $r$ can be written as $r = (n+2)j$ or $r-1=(n+2)j$ for some integer $j$. That is if $r/(n+2)$ or $(r-1)/(n+2)$ are integers (we call that $r$ or $r-1$ is divisible by $n+2$).