64
$\begingroup$

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$

$\endgroup$

closed as too broad by Dragonemperor42, GEdgar, Watson, kennytm, user223391 Aug 28 '16 at 13:55

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 23
    $\begingroup$ Check this out. $\endgroup$ – Eff Aug 21 '16 at 22:10
  • 8
    $\begingroup$ For $-1,-1,1,-1,-1,1,\ldots$ you can use $(-1)^{n^2\bmod 3}$. $\endgroup$ – user236182 Aug 21 '16 at 22:11
  • 4
    $\begingroup$ 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. $\endgroup$ – fleablood Aug 21 '16 at 23:13
  • 9
    $\begingroup$ 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. $\endgroup$ – JiK Aug 22 '16 at 12:00
  • 2
    $\begingroup$ @GEdgar Yes, I find Creating an alternating sequence of positive and negative numbers. $\endgroup$ – Jeppe Stig Nielsen Aug 22 '16 at 18:22

18 Answers 18

147
$\begingroup$

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}}$$

$\endgroup$
  • 6
    $\begingroup$ nice observation! $\endgroup$ – BigbearZzz Aug 21 '16 at 22:35
  • 20
    $\begingroup$ 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. $\endgroup$ – Nij Aug 22 '16 at 0:26
  • 11
    $\begingroup$ $(-1)^\frac{\phi^n - (-\phi)^{-n}}{\sqrt{5}}$ $\endgroup$ – EMBLEM Aug 22 '16 at 19:12
  • $\begingroup$ I think it should be $F_n$ not $F_{n+1}$. $\endgroup$ – Taha Akbari Aug 23 '16 at 8:09
  • 2
    $\begingroup$ @Nij This is essentially just writing the sequence $(-1)^{F_{n}}$ using the recursive definition of Fibonacci numbers. $\endgroup$ – Carl Schildkraut Aug 23 '16 at 18:31
61
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ 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... $\endgroup$ – luk2302 Aug 22 '16 at 20:57
  • $\begingroup$ @luk2302 That's actually interesting because the first time I read the question I completely missed the first paragraph :P $\endgroup$ – BigbearZzz Aug 22 '16 at 21:00
49
$\begingroup$

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$.

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

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

$\endgroup$
  • $\begingroup$ Computationally the most efficient function here I think. Nice one. $\endgroup$ – tomsmeding Aug 22 '16 at 5:52
  • $\begingroup$ @tomsmeding Thanks! $\endgroup$ – Kay K. Aug 22 '16 at 6:00
  • $\begingroup$ @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 $\endgroup$ – phuclv Aug 23 '16 at 10:34
  • $\begingroup$ @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. $\endgroup$ – tomsmeding Aug 23 '16 at 16:23
  • 7
    $\begingroup$ computationally efficient is {1,1,-1}[n % 3] $\endgroup$ – kevin cline Aug 23 '16 at 21:19
33
$\begingroup$

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

$\endgroup$
26
$\begingroup$

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}$$

$\endgroup$
16
$\begingroup$

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.

$\endgroup$
14
$\begingroup$

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}$

$\endgroup$
  • 3
    $\begingroup$ Could also eliminate the last case and make the other two $x = 3k$, $x = 3k + 1$, etc. $\endgroup$ – Ben Millwood Aug 23 '16 at 6:41
  • 1
    $\begingroup$ Fine except for f(-1) calling an infinite loop. :) $\endgroup$ – Wildcard Aug 24 '16 at 1:34
  • 1
    $\begingroup$ @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'. $\endgroup$ – user63495 Aug 24 '16 at 5:08
  • $\begingroup$ @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. $\endgroup$ – PJTraill Aug 24 '16 at 22:08
9
$\begingroup$

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$.

$\endgroup$
8
$\begingroup$

How about:

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

$\endgroup$
7
$\begingroup$

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$

$\endgroup$
4
$\begingroup$

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

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

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

$\endgroup$
  • $\begingroup$ Why exponentiate when $(n\pmod 3)!$ gives an equivalent solution? $\endgroup$ – Nij Aug 22 '16 at 4:23
  • $\begingroup$ @Nij that's for the sequence 1,1,2,1,1. But I realized that you don't even need the factorial. $\endgroup$ – Pulsar Aug 22 '16 at 4:31
  • $\begingroup$ 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. $\endgroup$ – Nij Aug 22 '16 at 5:57
  • $\begingroup$ @Nij $2!=2$, not $4$. $\endgroup$ – Pulsar Aug 22 '16 at 11:44
  • $\begingroup$ I knew I was making some mistake there. Fortunately it doesn't change the result! $\endgroup$ – Nij Aug 22 '16 at 18:05
4
$\begingroup$

Yet another one:

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

$\endgroup$
  • $\begingroup$ 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$. $\endgroup$ – 6005 Aug 23 '16 at 2:59
  • 1
    $\begingroup$ @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$. $\endgroup$ – Mario Carneiro Aug 23 '16 at 17:48
  • $\begingroup$ @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. $\endgroup$ – 6005 Aug 23 '16 at 18:32
  • 1
    $\begingroup$ 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. $\endgroup$ – whitehat101 Aug 24 '16 at 2:00
  • 1
    $\begingroup$ @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. $\endgroup$ – Mario Carneiro Aug 24 '16 at 3:58
3
$\begingroup$

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)$

$\endgroup$
2
$\begingroup$

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,...\}$

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

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}$$

$\endgroup$
2
$\begingroup$

Another one:

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

$\endgroup$
2
$\begingroup$

my 2 cents, how about

$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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.