Recurrence relation of the following sequence?

53 Views Asked by At

This is the code:

for (unsigned int i = 0; i < n; ++i)
        if (i % 2 == 0)
            ++k;

And this is the output for when n and k start at 1 and 0 respectively:

\begin{array}{c|c} n & k\\\hline 1 & 1\\ 2 & 1\\ 3 & 2\\ 4 & 2\\ 5 & 3\\ 6 & 3\\ 7 & 4\\ 8 & 4\\ 9 & 5\\ 10 & 5\\ 11 & 6\\ 12 & 6\\ 13 & 7\\ 14 & 7\\ 15 & 8\\ 16 & 8\\ 17 & 9\\ 18 & 9\\ 19 & 10\\ 20 & 10\\ \end{array}

I need to express this as a sequence. Something like $a_n = ..$. How would I do this?

I know it would be something like $a_n = \frac{n}{2}$ when $n$ is even, and $a_n = \lceil\frac{n}{2}\rceil$ otherwise but is there a different way?

1

There are 1 best solutions below

5
On BEST ANSWER

The simple solution is as you mentioned

$$a_n=\left\lceil\frac n2\right\rceil$$

A different way of expressing this could be:

$$a_n=\frac n2+\frac12\!\left|\,\sin\left(\frac{\pi x}2\right)\right|$$

Derivation of above formula:

We start with the formula $$a_n=\left\lceil\frac n2\right\rceil$$ When $n$ is even this is equal to $$a_n=\frac n2$$ And when $n$ is odd this is equal to $$a_n=\frac n2+\frac12$$

This means that if we find a function $f(x)$ that is equal to $0$ if $n$ is even and $\frac12$ when $n$ is odd then the following would be true.

$$a_n=\frac n2+f(x)$$

To find such a function, I'll use the periodicy of $\sin x$ meaning if $k$ is an integer then $\sin(\pi k)=0$

Another fact about the sine function is that $\sin\left(\pi\cdot\!\!\left(k + \frac12\right)\right)=\pm1$ where the $\pm1$ that for some integers it's $1$ and for others it's $-1$

Using these two facts we can deduce that

$$\sin\left(\frac{\pi k}2\right)$$

is zero for even $k$ and $\pm1$ for odd $k$, take the absolute value and half it, you have our function $f(x)$

$$f(x)=\frac12\!\left|\,\sin\left(\frac{\pi x}2\right)\right|$$


Can also be written like this:

$$a_1=1$$ $$a_2=1$$ $$a_n=1+a_{n-2}$$