# Formula for alternating sequences

I am looking for a general formula for alternating sequences. I know that the formula $f(x)=(-1)^x$ gives the sequence $1,-1,1,-1,...$ but I want more a general formula; for example the function $f(a,b,x)$ which returns the series $a,b,a,b,a,b,...$ as $x$ increases.

So for example the function $f(3,5,x)$ returns the series $3,5,3,5,3,5,...$ What would such a function $f(a,b,x)$ be?

• You seem to be using "function" in the colloquial sense of a formula. Hirshy's answer uses "function" in the technical mathematical sense of a relation. Aug 6 '15 at 19:25
• $\tfrac12((a+b)-(-1)^n(a-b))$ Aug 6 '15 at 19:27

$$\frac{a+b}{2}+(-1)^{n-1}\cdot\frac{a-b}{2}\ \ (n=1,2,\cdots)$$ represents $a,b,a,b,\cdots$.

$f(a,b,x)=\tfrac12((a+b)-\cos(\pi x)(a-b))$

How about: $$f(n)=\begin{cases} 3 & n\equiv 1\mod 2 \\ 5 & n\equiv 0 \mod 2\end{cases}$$

This gives $f(n)=3$ for every odd $n$ and $f(n)=5$ for every even $n$.

Edit As pointed out in the comments: $$f(a,b,n)=\begin{cases} a & n\equiv 1 \mod 2 \\ b & n\equiv 0 \mod 2\end{cases}\quad (n=1,2,\dots)$$ will work for arbitrary $a,b\in\mathbb R$. This can be easily generalised to sequences with more than two different numbers: $$f(a,b,c,n)=\begin{cases} a & n\equiv 1 \mod 3 \\ b & n\equiv 2 \mod 3 \\ c & n\equiv 0 \mod 3\end{cases}$$ for a sequence with three different numbers and finally $$f(a_1,a_2,\dots, a_n,n)=\begin{cases} a_1 & n\equiv 1 \mod n \\ \vdots \\ a_n & n\equiv 0 \mod n\end{cases}$$ for $n$ different numbers.

• They mean for 3 and 5 to be arguments to the function, such that $f(1,2,x) = 1, 2, 1, 2, 1, 2$ and $f(9,11,x) = 9, 11, 9, 11, 9, 11$. Aug 6 '15 at 19:28
• @Axoren one can easily write this as $$f(3,5,n)=\begin{cases} 3 & n\equiv 1\mod 2 \\ 5 & n\equiv 0 \mod 2\end{cases}$$ if the numbers $3$ and $5$ should be included as arguments. Aug 6 '15 at 19:31
• Do you mean the following? $$f(a,b,n)=\begin{cases} a & n\equiv 1\mod 2 \\ b & n\equiv 0 \mod 2\end{cases}$$ Edit: Just saw your edit, that's what you meant. Aug 6 '15 at 19:32
• dang I just realised what you meant; I was to focused on the sequence given by Hector, that I didn't think this all the way through. Aug 6 '15 at 19:34
• @Axoren thank you for pointing that out, I edited my answer for a more useful/correct function. Aug 6 '15 at 19:39