How can I get sequence $4,4,2,4,4,2,4,4,2\ldots$ into equation?
How can I write an equation that expresses the nth term of the sequence:
$$4, 4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 2,\ldots$$
Solution 1:
How about $$x_n=\begin{cases} 4 &\text{if }n\equiv 0,1\:(\bmod 3)\\ 2 &\text{if }n\equiv 2\:(\bmod 3)\\ \end{cases}$$ assuming you start indexing from $0$.
Solution 2:
$$\frac{14}{3} - \frac{8}{3}\cos^2 (\frac{2 \pi n}{3})$$
--
Added: The original formula was typed late at night, and sufered from a couple of computational blunders; hopefuly the present formula is correct.
Of course, the square on the cosine is unnecessary (I only put it there because I thought, due to miscalculation, that it simplified the coefficients).
In some sense the more natural formula is the one without the squared cosine, namely
$$ \frac{10}{3} - \frac{4}{3}\cos(\frac{2 \pi n}{3})$$
(as noted by the OP below).
Note that the existence of such a formula is not accidental or without interest. It is an illustration of finite Fourier theory (or, if you prefer, character theory of the finite abelian group $\mathbb Z/3\mathbb Z$). In general, any function of $n$ that depends only on $n \bmod N$ can be written as a linear combination of the functions $e^{2 \pi i n /N}$.
The most familiar example is probably the formula $(-1)^n$ for the sequence $-1,1,-1,1,\ldots$.
Whether such a formula is ever computationally useful is outside my area of expertise, but there is no doubt about the theoretical utility of finite Fourier theory.
[See Lubin's answer for an answer more explictly in keeping with this remark.]
Solution 3:
$$ f(n) = \begin{cases} 4 \text{ if } n \equiv 0 \text{ or } 1 \text{ (mod 3)}\\ 2 \text{ if } n \equiv 2 \text{ (mod 3)} \end{cases} $$
Solution 4:
$$4-2\cdot\mathbf 1_{3\mid n}\qquad\text{or}\qquad 2+2\cdot\mathbf 1_{\gcd(3,n)=1}$$
Solution 5:
What about
$$a_n:=\left\{\begin{array}{}4\,,&\text{if}\,\;\;n\neq 0\pmod 3\\2\,,&\text{if}\,\;\;n=0\pmod 3\end{array}\right....?$$