25

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

M Turgeon
  • 10,419
ben
  • 2,155

17 Answers17

59

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

Alex Becker
  • 60,569
  • Assuming you only want to determine the nth entry of a sequence, this would be the most efficient way of calculating it. – dstibbe Sep 11 '12 at 09:18
  • 33
    Seriously? My highest upvoted non CW answer is this? – Alex Becker Sep 11 '12 at 17:41
  • 3
    ROFL. I'm guessing this is merely caused by people wanting to top the "approved" answer, since that answer is computationally very inefficient. – dstibbe Sep 11 '12 at 20:37
  • 4
    At least it is a better answer than the one offered by that lunkhead Austin Mohr. – MJD Sep 11 '12 at 22:29
  • @dstibbe: Dear dstibbe, Please see the edit to my answer for some justification for its existence beyond amusement. Regards, – Matt E Sep 12 '12 at 00:42
  • 1
    @Matt E I'm quite familier with the Fourier theory. However, if someone is not going to use the formula for anything else besides determing the value of the nth position, then it is overly complex (and thus inefficient) when above formula provides the same. – dstibbe Sep 12 '12 at 08:39
  • I'm still salivating over this answer. So much reputation... – Austin Mohr Sep 12 '12 at 11:45
  • @AustinMohr It's not as good as it looks. Most of the upvotes were in the same day, so I hit the reputation cap fast. – Alex Becker Sep 12 '12 at 18:28
  • @dstibbe: Dear dstibbe, Fair enough. Regards, – Matt E Sep 14 '12 at 02:51
40

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

Matt E
  • 123,735
  • see http://mathoverflow.net/questions/106560/philosophy-behind-mochizukis-work-on-the-abc-conjecture/106658#106658 – Will Jagy Sep 11 '12 at 02:47
  • @Will: Dear Will, Thanks; I had seen it, and it's great! Regards, – Matt E Sep 11 '12 at 02:50
  • 3
    @MattE: Exactly what I need, BUT, this seems to work better: $$4(\frac{5}{6}-\frac{1}{3}\cos(\frac{2\pi n}{3} ))$$ – ben Sep 11 '12 at 05:14
  • 1
    This seems to give 5, 5, 2, 5, 5, 2... http://www.wolframalpha.com/input/?i=6%E2%88%924cos%5E2%282%CF%80n%2F3%29+where+n%3D1..12 – Deebster Sep 11 '12 at 14:04
  • 7
    Amazing how the most computationally inefficient formula is the most up voted. – asmeurer Sep 11 '12 at 18:38
  • @asmeurer it's not the most upvoted ! – wim Sep 12 '12 at 00:16
  • @Deebster: Dear Deebster, Thanks, there is a typo (an unnecessary squaring of the cosine), and also a sign error. (The original answer was typed late at night!) Regards, – Matt E Sep 12 '12 at 00:24
  • 4
    @asmeurer: Dear asmuerer, You might find the formula dubious, but in fact it is an example of finite Fourier theory: a function depending on the congruence class of $n$ mod $N$ admits a Fourier expansion. It is analogous to writing $-1,1,-1,1, \ldots$ as $(-1)^n$, and is useful in some contexts (at least theoretical ones, which is what I am more familiar with). Regards, – Matt E Sep 12 '12 at 00:30
  • @Deebster: Dear Deebster, Additional computational blunders introduced, removed, and hopefully all is now correct. Please feel free to let me know if I've still got it screwed up. Regards, – Matt E Sep 12 '12 at 01:11
  • @MattE: Looks good to me! http://www.wolframalpha.com/input/?i=14%2F3%E2%88%928%2F3cos%5E2%282%CF%80n%2F3%29+where+n%3D1..12 – Deebster Sep 12 '12 at 09:09
  • MattE: most computationally inefficient $\ne$ dubious. In fact I fail to see the word dubious (even the idea) in @asmeurer's comment. – Did Sep 13 '12 at 05:34
  • 1
    It certainty is mathematically interesting. But if the user wanted it for a program (possible, but it wasn't stated), then @AlexBecker's answer would be better. I've seen very computationally inefficient answers to sequence problems on math SE for problems where the question specifically stated it was for a computer program. See for example http://math.stackexchange.com/questions/162495/whats-the-formula-of-this-sequence/162540#comment374830_162540. – asmeurer Sep 13 '12 at 17:11
  • @asmeurer: Dear asmeurer, Thanks for your comment and elaborating on the computational point of view. Best wishes, – Matt E Sep 14 '12 at 02:45
  • @did: Dear did, You're right that dubious was not the best choice of word (I mainly chose it for its colour), but asmeurer did express amazement. I just wanted to indicate that there can be other considerations besides computational efficiency; I didn't intend to cause offense when doing so. Best wishes, – Matt E Sep 14 '12 at 02:47
  • @asmeurer: Dear asmeurer, Please see my previous comment addressed to @did; I'm sorry to have put words in your mouth with my choice of the term "dubious" in describing your eaerlier comment. Best wishes, – Matt E Sep 14 '12 at 02:49
  • @MattE OK. $ $ $ $ – Did Sep 14 '12 at 04:58
  • Perhaps this is more computationally expensive but I have to say it's more elegant and less "magic-number" than the alternative mod-based approach. If one needed to modify a part of the pattern specifically this is much easier to work with than a mystical array. – zwerdlds Mar 22 '13 at 18:12
29

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

Austin Mohr
  • 25,662
  • (+1) You were first; that has to count for something :-) – robjohn Jul 29 '13 at 19:45
  • 4
    @robjohn Indeed... by 12 seconds. I did not check the timestamps and decided that Alex must have been first to get that many upvotes. Oh well, at least my meta post brought some justice here. :) – 40 votes Jul 29 '13 at 20:14
14

$$4-2\cdot\mathbf 1_{3\mid n}\qquad\text{or}\qquad 2+2\cdot\mathbf 1_{\gcd(3,n)=1}$$

Did
  • 279,727
12

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

DonAntonio
  • 211,718
  • 17
  • 136
  • 287
  • Or 2+ 2*(n %% 3 != 0) . (Not sure of the local convention for modulo remainder so used the R operator) – DWin Sep 11 '12 at 16:43
10

$a_{n+2} = |a_{n+1} - a_n| + 2$, where $a_1 = a_2 = 4$.

Théophile
  • 24,627
8

The "quotients" $a_j$ of the simple continued fraction for $$ \frac{17 + \sqrt {442}}{9}. $$

See PURELY PERIODIC

Will Jagy
  • 139,541
7

$$\Large 2^{2-0^{(n \text{ mod } 3)}}$$

Pedro
  • 122,002
6

Try $a_n=2^{1+\lceil n/3 \rceil - \lfloor n/3 \rfloor}$, where $\lceil n/3 \rceil$ is the least integer $\geq n/3$ and where $\lfloor n/3 \rfloor$ is the greatest integer $\leq n/3$. Then, if 3 divides $n$, you get $2^1$; if it doesn't, you get $2^2$.

Tarnation
  • 1,305
  • 10
  • 18
6

$$x_n= 2+ 3 \left\{ \frac{n}{3} \right\} + 3\left\{ \frac{n}{3}\right\}\left(2- 3\left\{ \frac{n}{3} \right\}\right) \,.$$

where $\{ \}$ denotes the fractional part.

MJD
  • 65,394
  • 39
  • 298
  • 580
N. S.
  • 132,525
5

The On-Line Encyclopedia of Integer Sequences, is always a good place to start looking for them. One often needs to search for one excluding constant multiplication factor and/or drop a few initial terms. And/or add a constant factor (as Theóphile points out below)

For this one we can use 1,2,2,1,2,2,1,2,2,... (http://oeis.org/A130196), drop the initial "1" and multiply by 2

5

Mimicking the cosine answer of @Matt E, I suggest setting $\omega=(1+\sqrt{-3})/2$ and taking $a_n=2+2|\omega^n-\omega^{2n}|/\sqrt3$.

Lubin
  • 62,818
  • Dear Lubin, Indeed this is what I had in mind (as I indicated in an edit to my answer, which also seemed to be rife with typos; hopefully they are now fixed and agree with your answer). Regards, – Matt E Sep 12 '12 at 01:05
4

$$ x_n = 3 + (-1)^{((n+2) mod 3)} $$

dan
  • 162
1

$x_n = 4(n^2 \bmod 3)+2(1-(n^2 \bmod 3))=2+2(n^2 \bmod 3)$, assuming you start indexing from $1$.

lhf
  • 216,483
1

$2n^2+4n+4 \pmod 6$ for $n \geq 0$.

1

$$3+ { \left( -1 \right) }^{ (n+1)\mod 3 } ,\; \mathbb{for} \; n = 1,2,...$$

newzad
  • 4,855
0

$x_n= \begin{cases} 4,&n=0,1\\ (x_{n-2} + x_{n-1})\,\bmod 4 + 2,&n\ge2 \end{cases} $

Brian M. Scott
  • 616,228
NotGaeL
  • 101