Wednesday 28 May 2014

Formula for all terms in a sequence.



I am looking to find an algorithm for a repeating pattern, in order to solve a programming problem. I need a formula for all terms of the following sequence:



0, 2, 4, ..., 0, 2, 4, ...


The sequence would start at 0, and then increment by 2 until reaching 4 and then repeat itself with 0. I have worked with similar patterns, and they usually involve the modulus operator, and the number of repeating terms. For example, from what I understand I could get a repeating pattern just by applying the modulo to the index by the number of repeating terms.



This seems like it should be simple enough, but is eluding me and I do not have a mathematics background.



Answer



You could simply say that the $i$-th element of the sequence is the remainder of when you divide $2\cdot i$ by $6$.



That is, if you count $0$ as the zero-th element. If it's the first, just replace the $i$ with $(i-1)$.






For example:





  • $0$ is the remainder when you divide $2\cdot 0$ by $6$

  • $2$ is the remainder when you divide $2\cdot 1$ by $6$

  • $4$ is the remainder when you divide $2\cdot 2$ by $6$

  • $0$ is the remainder when you divide $2\cdot 3$ by $6$

  • $2$ is the remainder when you divide $2\cdot 4$ by $6$

  • $4$ is the remainder when you divide $2\cdot 5$ by $6$



So, if you want to calculate the $i$-th element in, say, Python, you would calculate it as




i_th_element = (2 * i) % 6

No comments:

Post a Comment

real analysis - How to find $lim_{hrightarrow 0}frac{sin(ha)}{h}$

How to find $\lim_{h\rightarrow 0}\frac{\sin(ha)}{h}$ without lhopital rule? I know when I use lhopital I easy get $$ \lim_{h\rightarrow 0}...