Logical treatment for the oscillatory sequence 1, 2, 3, 4, 3, 2, 1, 2, . . . to find any term and a computer program to assist the operation

May 28, 2021  1611 words 8 mins read  Join the Discussion

In this post, I’m going to talk about the topic that inspired me to computational physics.

Let me take you back to when I was at the beginning of my B.Sc. physics in the second year. One of my friends asked me a question on a sequence which is like this: $1, 2, 3, 4, 3, 2, 1,2, \ldots$ (OEIS A028356). He asked, “Is it possible to find an efficient method for its general term?”. He cautiously mentioned that “Please don’t try to think of an iteration method!”. What he meant by iteration method is that if you iterate over the sequence and you know say 3 then, you instantly know 4. The reason he said it is because the method can consume more CPU time as you iterate through a huge term number (say one googol). Good question, I said! Then, we both started to think about this problem. My friend was sketching some mathematical construction, and I was staring at the sequence. After an hour, I realized that I found an answer. So, let’s see what I found.

First of all, we need to draw a graph wherein on the x-axis we will have term numbers ($N_{i}$), and in the y-axis, we write terms ($t_{N}$).

70pc

Now, we draw the line plot for our sequence.

70pc

Then, we draw the $y = t_{N}$ lines.

70pc

Do you notice something in the above plot? Perhaps you wait for some minutes and think before reading the next sentence. Can you see something related to arithmetic progression?

If you can see it then, you may have the aha moment same as mine; when I realized that any oscillatory sequence is related to some arithmetic sequences.

If you look at $y = 1$ line then, you see the distance between two consecutive terms has the same constant common difference. This suggests that we can get one arithmetic sequence.

70pc

Similarly, if you see $y = 2$ line then, you can get another arithmetic sequence. But in this sequence, you can extract two more sequences (even and odd sequences) because there are two common differences at even and odd term number.

70pc

Likewise, you can also see these.

70pc

70pc

It means we have four new sequences using one sequence that is easy to deal with them.

$t_{N}$Lying on the $y = t_{N}$ lineGeneral term
$1$$1, 7, 13, 19, \ldots$ to $i$ terms$t_{i}$
$2$$2, 6, 8, 12, 14, 18, \ldots$ to $j$ terms$t_{j}$
$3$$3, 5, 9, 11, 15, 17, \ldots$ to $k$ terms$t_{k}$
$4$$4, 10, 16, \ldots$ to $l$ terms$t_{l}$

We are going to find the general terms for the above newly formed sequences.

The general term for the sequence $S_{1} = 1, 7, 13, 19, \ldots$ can be found by

$$ \begin{align*} t_{i} &= 1 + (i - 1)6 \\ \therefore t_{i} &= 6i - 5 \quad\text{such that } i = 1, 2, 3, \ldots. \end{align*} $$

Similarly, the general term for the sequence $S_{4} = 4, 10, 16, \ldots$ can be found by

$$ \begin{align*} t_{l} &= 4 + (l - 1)6 \\ \therefore t_{l} &= 6l - 2 \quad\text{such that } l = 1, 2, 3, \ldots. \end{align*} $$

But for the sequence $S_{2} = 2, 6, 8, 12, 14, 18, \ldots$, we have to work out a little bit more. So let’s do it this way, you first make series of this sequence then, write the same series in the next line but shift by one term. Finally, you subtract both of them. i.e.

$$ \begin{align*} \text{Series of } S_{2} &= 2 + 6 + 8 + 12 + 14 + 18 + \ldots + t_{j - 1} + t_{j} \\ \text{Series of } S_{2} &= \hspace*{-0.1cm}\downarrow + 2 + 6 + 8 + 12 + 14 + 18 + \ldots + t_{j - 1} + t_{j}\\ - & \\ \hline 0 &= 2 + 4 + 2 + 4 + 2 + 4 + ... + (t_{j} − t_{j − 1}) − t_{j}\\ \therefore t_{j} &= 2 + 4 + 2 + 4 + 2 + 4 + \ldots \text{ to } j \text{ terms.} \end{align*} $$

When $j$ is even,

$$ \begin{align*} t_{j} &= (2 + 2 + 2 + \ldots \text{ to } (j/2) \text{ terms}) + \\ &\hspace{0.5cm} (4 + 4 + 4 + \ldots \text{ to } (j/2) \text{ terms})\\ &= 2 \times (j/2) + 4 \times (j/2) \\ \therefore t_{j} &= 3j. \end{align*} $$

When $j$ is odd,

$$ \begin{align*} t_{j} &= 2 \times ((j + 1)/2) + 4 \times ((j − 1)/2)\\ &= (j + 1) + 2 \times (j - 1) \\ \therefore t_{j} &= 3j - 1. \end{align*} $$

Similarly goes for the sequence $S_{3} = 3, 5, 9, 11, 15, 17,\ldots $,

$$ \begin{align*} \text{Series of } S_{3} &= 3 + 5 + 9 + 11 + 15 + \ldots + t_{k - 1} + t_{k} \\ \text{Series of } S_{3} &= \hspace*{-0.1cm}\downarrow + 3 + 5 + 9 + 11 + 15 + \ldots + t_{k - 1} + t_{k}\\ - & \\ \hline 0 &= 3 + 2 + 4 + 2 + 4 + 2 + ... + (t_{k} − t_{k − 1}) − t_{k}\\ \therefore t_{j} &= 3 + (2 + 4 + 2 + 4 + 2 + 4 + \ldots \text{ to } k \text{ terms).} \end{align*} $$

When $k$ is odd,

$$ \begin{align*} t_{k} &= 3 + (2 + 2 + 2 + \ldots \text{ to } ((k -1 )/2) \text{ terms}) + \\ &\hspace{0.5cm} (4 + 4 + 4 + \ldots \text{ to } ((k - 1)/2) \text{ terms})\\ &= 3 + 2 \times ((k - 1)/2) + 4 \times ((k - 1)/2) \\ \therefore t_{j} &= 3k. \end{align*} $$

When $k$ is even,

$$ \begin{align*} t_{k} &= 3 + 2 \times (((k - 1) + 1)/2) + 4 \times (((k - 1) − 1)/2)\\ &= 3 + k + 2 \times (k - 2) \\ \therefore t_{k} &= 3k - 1. \end{align*} $$

Let’s summarize what we have been so far. The general terms formulae are

  • $t_{i} = 6i - 5$ such that $i = 1, 2, 3,\ldots$
  • When j is even, $t_{j} = 3j $ such that $j = 1, 2, 3,\ldots$
  • When j is odd, $t_{j} = 3j - 1$
  • When k is even $t_{k} = 3k - 1$ such that $k = 1, 2, 3,\ldots$
  • When k is odd $t_{k} = 3k$
  • $t_{l} = 6l - 2$ such that $l = 1, 2, 3,\ldots$.

You may notice that $t_{i}, t_{j}, t_{k},$ and $t_{l}$ are just a term number ($N$) in our original sequence. This means $t_{x} = N$ where $x \in i, j, k, l$. So, we need to invert the general terms by adding some conditions.

For the sequence $S_{1}$,

$$ \begin{align*} t_{i} &= 6i - 5 = N \\ \therefore i &= \frac{N + 5}{6}. \end{align*} $$

Condition: If $i \in \mathbb{N}$ then, the $N^{\text{th}}$ term is 1.

For the sequence $S_{2}$,

$$ \begin{align*} t_{j} &= 3j = N \\ \therefore j &= \frac{N}{3}. \end{align*} $$

Condition: If $j \in \mathbb{N}$ and $j$ is even then, the $N^{\text{th}}$ term is 2.

Similarly,

$$ \begin{align*} t_{j} &= 3j - 1 = N \\ \therefore j &= \frac{N + 1}{3}. \end{align*} $$

Condition: If $j \in \mathbb{N}$ and $j$ is odd then, the $N^{\text{th}}$ term is 2.

For the sequence $S_{3}$,

$$ \begin{align*} t_{k} &= 3k - 1 = N \\ \therefore k &= \frac{N + 1}{3}. \end{align*} $$

Condition: If $k \in \mathbb{N}$ and $k$ is even then, the $N^{\text{th}}$ term is 3.

Similarly,

$$ \begin{align*} t_{k} &= 3k = N\\ \therefore k &= \frac{N}{3}. \end{align*} $$

Condition: If $k \in \mathbb{N}$ and $k$ is odd then, the $N^{\text{th}}$ term is 3.

For the sequence $S_{4}$,

$$ \begin{align*} t_{l} &= 6l - 2 = N \\ \therefore l &= \frac{N + 2}{6}. \end{align*} $$

Condition: If $l \in \mathbb{N}$ then, the $N^{\text{th}}$ term is 4.

Up to this point, you may notice that it is tedious to test all the conditions by hand. So, we will take the help of a computer to do this job. But before that, it will be even helpful if we define some axioms for the logic we found earlier. The axioms goes like this:

  • If all (n − 1) tests fail then, the last test n must be true where, n = total number of tests.
  • If one test passed then, all other remaining tests must fail.

Now, we have all the ingredients to write the computer program. At the same time, we will use our axioms. I’m using Python v3.x to implement it. I assume you are familiar with this language. The program will look like this:

#! /usr/bin/python3

N = int(input("Enter the value of N as you like: "))
if (N + 5) % 6 == 0:
  print("The {}th term is 1".format(N))
elif N % 3 == 0:
  if (N / 3) % 2 == 0:
    print("The {}th term is 2".format(N))
  else:
    print("The {}th term is 3".format(N))
elif (N + 1) % 3 == 0:
  if ((N + 1) / 3)% 2 == 0:
    print("The {}th term is 3".format(N))
  else:
    print("The {}th term is 2".format(N))
else:
  print("The {}th term is 4".format(N))

As you can realize, we are only testing the condition instead of iterating over all the terms. Thus, I conclude that this method is more efficient than the iteration method.

After solving this problem, I asked myself, “Wouldn’t it be interesting if I can solve physics problems using the help of computer science?”. From that point, I started to improve my knowledge of computer science.

Any feedback?

If you guys have some questions, comments, or suggestions then, please don't hesitate to shot me an email at [firstname][AT]physicslog.com or comment below.

Liked this post?

If you find this post helpful and want to show your appreciation, I would appreciate "a Coffee or Nepali Chi·ya (चिया)". It's a small gesture that can make my day!

Want to share this post?

  • Damodar Rajbhandari
    Written by Damodar Rajbhandari, a PhD candidate in the Mathematical Physics at the School of Mathematics & Statistics, University of Melbourne, Australia.
Related Posts