# Is there a simple function that generates the series; $1,1,2,1,1,2,1,1,2…$ or $-1,-1,1,-1,-1,1…$ [closed]

I'm thinking about this question in the sense that we often have a term $(-1)^n$ for an integer $n$, so that we get a sequence $1,-1,1,-1...$ but I'm trying to find an expression that only gives every 3rd term as positive, thus it would read; $-1,-1,1,-1,-1,1,-1,-1...$

Alternatively a sequence yielding $1,1,2,1,1,2,1,1,2...$ could also work, as $n$ could just be substituted by it in $(-1)^n$

• Check this out. – Eff Aug 21 '16 at 22:10
• For $-1,-1,1,-1,-1,1,\ldots$ you can use $(-1)^{n^2\bmod 3}$. – user236182 Aug 21 '16 at 22:11
• Well, f(n) = 1 if $3|n$ and f(n) = -1 if $3 \not \mid n$ is a perfectly legitimate function. For something less "if then" $(-1)^{\lceil (n/3 - \lfloor n/3\rfloor)\rceil}$ but that just point the "if then" burden where no-one sees it. Still there are better ones. – fleablood Aug 21 '16 at 23:13
• Why do you need such a function? If for fun, then the answers are good. If for short notation (for whatever reason), then some are not very convenient. If for easy readability, then most answers are horrible. – JiK Aug 22 '16 at 12:00
• @GEdgar Yes, I find Creating an alternating sequence of positive and negative numbers. – Jeppe Stig Nielsen Aug 22 '16 at 18:22

Let $F_n$ be the Fibonacci sequence $0,1,1,2,3,5,8,13,21,34,...$ then a possible sequence is $$(-1)^{F_{n+1}}$$

• nice observation! – BigbearZzz Aug 21 '16 at 22:35
• I like the specific use of Fibonacci. I think a recursive function might even give the OP a direct route, say starting with $1,-1,...$ and then using product of previous two entries. – Nij Aug 22 '16 at 0:26
• $(-1)^\frac{\phi^n - (-\phi)^{-n}}{\sqrt{5}}$ – EMBLEM Aug 22 '16 at 19:12
• I think it should be $F_n$ not $F_{n+1}$. – Taha Akbari Aug 23 '16 at 8:09
• @Nij This is essentially just writing the sequence $(-1)^{F_{n}}$ using the recursive definition of Fibonacci numbers. – Carl Schildkraut Aug 23 '16 at 18:31

How about $$a_n=\frac 23\cos\left(\frac{2\pi n}3\right)+\frac 43\$$

• Was wondering for a few minutes now why I did not see how this gets to -1 and why it returns 2 until I re-read the question, in particular the second paragraph. I thought my math was failing me completely... – luk2302 Aug 22 '16 at 20:57
• @luk2302 That's actually interesting because the first time I read the question I completely missed the first paragraph :P – BigbearZzz Aug 22 '16 at 21:00

If you want something purely in terms of elementary operations, you could use the closed form:

$$\frac{2}{3}\left(-\frac{1}{2} + \omega^n + \omega^{2n}\right)$$

Where $\omega=\frac{-1+\sqrt{3}i}{2}$ is a complex cube root of unity. When $3|n$ we get $\omega^n =\omega^{2n} = 1$. On the other hand, when $n$ is not divisible by $3$, $\omega^n$ and $\omega^{2n}$ will be the two roots of the polynomial:

$$z^2 + z + 1$$

And from Vieta we get $\omega^n + \omega^{2n} = -1$.

• This is not obvious from the form of the expression, but this is the same function (extended to complexes) as @BigbearZzz's. – Mario Carneiro Aug 23 '16 at 17:43
• @MarioCarneiro Indeed it is, nice observation. – sbares Aug 23 '16 at 20:11

$$-1+2\left \lfloor {\frac n 3} \right \rfloor -2\left\lfloor \frac {n-1}3 \right\rfloor$$ , where $n$ starts from $1$.

• Computationally the most efficient function here I think. Nice one. – tomsmeding Aug 22 '16 at 5:52
• @tomsmeding Thanks! – Kay K. Aug 22 '16 at 6:00
• @tomsmeding I don't think it's computionally efficient, because you need 2 divisions, whereas many of the below solutions need only one modulo operation – phuclv Aug 23 '16 at 10:34
• @LưuVĩnhPhúc Good point, I mentally skipped the ones using modulo because I thought them "cheating", but of course they're easier for a computer. – tomsmeding Aug 23 '16 at 16:23
• computationally efficient is {1,1,-1}[n % 3] – kevin cline Aug 23 '16 at 21:19

$a(0) = a(1) = -1$
$a(n) = a(n-1) \times a(n-2)$

You can check out OEIS which gives you several ways to generate your sequence.

For example:

$$a_n=2-((n+1)^2 \pmod 3)$$

$$a_n=\frac{4}{3} - \frac{\cos(2\pi n/3)}{3} - \frac{\sin(2\pi n/3)}{\sqrt3}$$

You could use a simple trig function whose period is $3$. We can therefore try a function of the form $$f(x)=a\cos\left(\frac{2\pi x}{3}\right)+b$$

Substituting the coordinates $(0,2),(1,1)$ gives the values of $a,b$ and we end up with $$f(x)=\frac 23\cos\left(\frac{2\pi x}{3}\right)+\frac 43$$ and this generates the required sequence.

No operators!

$f(x) = \begin{cases} -1 & \text{if}~x=0\\ -1 & \text{if}~x=1\\ 1 & \text{if}~x=2\\ f(x-3) & \text{otherwise}\\ \end{cases}$

• Could also eliminate the last case and make the other two $x = 3k$, $x = 3k + 1$, etc. – Ben Millwood Aug 23 '16 at 6:41
• Fine except for f(-1) calling an infinite loop. :) – Wildcard Aug 24 '16 at 1:34
• @Wildcard there is no such thing as an infinite loop. OP asked for a sequence and sequences have a beginning (typically 0 or 1). Several of the other answers have the same 'issue'. – user63495 Aug 24 '16 at 5:08
• @Wildcard: from $f(2-3)=f(2)$ you deduce that $f(-1)=1$. If you really want to programme it, you have to stop briefly and think. – PJTraill Aug 24 '16 at 22:08

Let $\omega \neq 1$ be a cubic root of unity. We have that $1 + \omega + \omega^2 = 0$, i.e. $\omega + \omega^2 = -1$. Also, $w^{-1} = \omega^2$ and $\omega^3 = 1$.

Put $a_n = \omega^n + \omega^{-n}$. We get the sequences

\begin{eqnarray*} a_0 &=& \omega^{0} + \omega^{0} = 2 \\ a_1 &=& \omega^{1} + \omega^{-1} = \omega^{1} + \omega^{2} = -1 \\ a_2 &=& \omega^{2} + \omega^{-2} = \omega^{2} + \omega^{1} = -1 \\ \end{eqnarray*}

and then the sequence repeats.

If you put $b_n = \frac{2 a_n -1}{3}$, then you get $1, -1, -1, 1, -1, -1, \cdots$.

$$(n\bmod3)^2-3(n\bmod3)+1$$

Iverson bracket notation makes it easy to say exactly what you mean here:

$a_n = 2\cdot[\text{$n$is a multiple of$3$}] - 1$

or, shorter but more (probably excessively) confusing:

$a_n = 2[3\mid n] - 1$

$$(-1)^{(n\,\text{mod}\, 3)!}$$

Edit. a simpler solution, without a factorial and starting with $n=2$:

$$(-1)^{(n\,\text{mod}\, 3)+1}$$

• Why exponentiate when $(n\pmod 3)!$ gives an equivalent solution? – Nij Aug 22 '16 at 4:23
• @Nij that's for the sequence 1,1,2,1,1. But I realized that you don't even need the factorial. – Pulsar Aug 22 '16 at 4:31
• The factorial does matter if used as exponent, but not when used directly, to be clear. 0, 1, 2 gives 1, -1, 1. But 0!, 1!, 2! gives 1, 1, 4. Used as exponents they then return 1, -1, 1 versus -1, -1, 1. – Nij Aug 22 '16 at 5:57
• @Nij $2!=2$, not $4$. – Pulsar Aug 22 '16 at 11:44
• I knew I was making some mistake there. Fortunately it doesn't change the result! – Nij Aug 22 '16 at 18:05

Yet another one:

$$1 + \left\lfloor \frac{n \bmod 3}{2} \right\rfloor$$

• You might consider specifying the meaning by "mod" in this context. In mathematics, "mod" is almost never a function of two arguments like it is in programming and in this answer; instead, it's an equivalence relation. A common alternative is to make a remainder function, $\text{rem}(n,3)$ which returns remainder of $n$ on division by $3$. – 6005 Aug 23 '16 at 2:59
• @6005 This is a genuine usage of the \bmod modulo function, which does appear in pure mathematics occasionally. This is not to be confused with the \pmod modulo qualifier on equivalence, which is the standard way to talk about modular equivalence. There's no need to make a separate notation for this usage. FYI, the binary modulo function is natural once you have floor: $a\bmod b=a-b\lfloor a/b\rfloor$. – Mario Carneiro Aug 23 '16 at 17:48
• @MarioCarneiro Well, it's fine if you disagree. I don't see how the unmotivated formula "$a - b \lfloor a / b \rfloor$" is "natural". I have taught discrete math classes and you really have to stress, pedagogically at least, that mod is not a binary function. People write things like $a \text{ mod } b = c$ instead of $a \equiv c \pmod{b}$, which is wrong unless $c$ is actually between $0$ and $b-1$. So it does throw a lot of people off, and in my opinion it's bad notation. I'm sure it does appear in some math books, but not in any undergraduate or graduate course I took. – 6005 Aug 23 '16 at 18:32
• Binary modulo is the most common use. Half the answers on this page make use of it without hesitation or definition. Outside of a discrete mathematics classroom, $a \bmod b = c$ is meaningful and correct. – whitehat101 Aug 24 '16 at 2:00
• @6005 I only mean that binary mod is within the set of functions generated by the field operations plus $\lfloor\cdot\rfloor$, so in a sense having one implies having the other as well. I don't see the expression $a\bmod b=c$ very often, but when I do I would understand it just as you have described - $c$ is the unique number equivalent to $a$ in the range $[0,b)$. Obviously this is quite different from $a\equiv c\pmod b$, but they both have their uses. – Mario Carneiro Aug 24 '16 at 3:58

This works for positive and negative integers of $x$ and produces a sequence of $1,1,2,1,1,2,...$

$y = 2 - (x^2 \text{ mod } 3)$

Or for the sequence $-1,-1,1,-1,-1,1,...$

$y = 1 - 2(x^2 \text{ mod } 3)$

Here is my proposal, for $n \ge 1$:

$$1+\left\lfloor \frac{1}{n^{(n \pmod {3})}} \right\rfloor - \left\lfloor \frac{1}{n}\right\rfloor$$

It will provide the sequence: $\{1,1,2,1,1,2,...\}$

• @CiaPan thanks for the change, I did not know that formatting option! – iadvd Aug 22 '16 at 6:50

Lemma:fibonacci numbers generates:

$odd-odd-even-odd-odd-even-odd-odd-\dots$

proof:

If

$F_i=odd$

$F_{i+1}=odd$

Then we have:

$F_{i+2}=even$

$F_{i+3}=odd$

$F_{i+4}=odd$

then the system reapeates.

It gives the formula:

$(-1)^{F_{n}}$

This gives the closed form:

$$(-1)^{(\frac{1}{\sqrt{5}}((\frac{1+\sqrt{5}}{2})^{n+1}-(\frac{1-\sqrt{5}}{2})^{n+1})}$$

Which is really ugly.I will introduce Lucas numbers to make this better.

Lucas numbers look like fibonacci numbers but $F_1=1$ and $F_2=3$.

Which gives the same odd and even resault.Then we can write:

$(-1)^{L_n}$

That gives the closed form:

$$(-1)^{(\frac{1+\sqrt{5}}{2})^n+(\frac{1-\sqrt{5}}{2})^n}$$

Another one:

$$2\times((n+1\bmod3) \bmod 2)-1$$

$a_{n+1}=(-1)^{r(a_n)(1-a_n)}$ where $a_0 = 1$ and $r \in \{1,2,3\}$ which is similar to the logistic map in general I think that using a one dimensional chaotic polynomial could help.