How to derive a generating function for the following series

234 Views Asked by At

Given an integer n how would derive a function fn that is without conditional statements, does not use floor or ceiling functions and returns an integer which has the following relationship to n:

    n = {1,  2,  3,  4,  5,  6, ...}
fn(n) = {0, +1, -1, +2, -2, +3, ...}

I know the answer (I didn't want to pollute your answers by sharing it), but I can't seem to derive it myself logically - so, this question is about how to derive the function, not how the function looks like.

1

There are 1 best solutions below

2
On BEST ANSWER

Clearly a factor of $(-1)^n$ gets you the right algebraic sign, so the real problem is getting the absolute value of $f(n)$ to come out right. You want it to be $\frac{n}2$ when $n$ is even and $\frac{n-1}2$ when $n$ is odd; you can get this easily if you can find a function $g(n)$ that returns $0$ when $n$ is even and $-1$ when $n$ is odd. The function $g(n)=(-1)^n$ gives two different constant values depending on the parity of $n$, so we try to modify it: $g(n)=(-1)^n-1$ produces $0$ when $n$ is even and $-2$ when $n$ is odd, so dividing that by $2$ does the trick. Now put the pieces together:

$$f(n)=\frac{(-1)^n}2\left(n+\frac{(-1)^n-1}2\right)\;,$$

which can of course be simplified, e.g., to

$$f(n)=\frac{(-1)^n(2n-1)+1}4\;.$$