Number of functions $f\colon\{1,2,3,\dots,n\} \to \{1,-1,i,-i\}$ satisfying a certain condition What should I do here? I don't even know where to start from. Please help me by giving me a hint.

Find how many are the functions: $f: \{1,2,3,\dots,n\} \to \{1,-1,i,-i\}$, where $n \geq 2$, such that
$$f(k)+f(k+1)\neq 0 \, \, \, (k=1,2,3,\cdots,n-1), \quad \quad \quad f(n)+f(1)\neq 0$$

I find $n=2$ then number of $f$ is $12$.
For $n=3$ then number of $f$ is $28$.
 A: Let $n$ be a positive integer.  A function $f:\{1,2,\ldots,n\}\to\{1,-1,\text{i},-\text{i}\}$ is said to be $n$-admissible if $f(k)+f(k+1)\neq 0$ for all $k=1,2,\ldots,n-1$ and $f(n)+f(1)\neq 0$.  Let $a_n$ denote the number of $n$-admissible functions.
Fix an integer $n\geq 3$.  First, there are $4\cdot 3^{n-2}$ functions $g:\{1,2,\ldots,n-1\}\to\{1,-1,\text{i},-\text{i}\}$ such that $g(k)+g(k+1)\neq 0$ for $k=1,2,\ldots,n-2$.  (This is an easy counting job: $g(1)$ has $4$ possible values, and $g(k)$ has only $3$ possible values for $k=2,3,\ldots,n-1$.)  Among these $g$'s, there are $a_{n-2}$ of them with $g(n-1)=g(1)$, and we can extend such a function $g$ to an $n$-admissible function $f$ in $3$ ways.  For the other $g$'s, there are $4\cdot 3^{n-2}-a_{n-2}$ of them, and we can extend such a function $g$ to an $n$-admissible function $f$ in only $2$ ways.  Consequently, $$a_n=3\cdot a_{n-2}+2\cdot\left(4\cdot 3^{n-2}-a_{n-2}\right)=a_{n-2}+8\cdot 3^{n-2}\,.$$
As $a_1=4$ and $a_2=12$, we get 
$$a_n=3^n+(-1)^n+2=\left\{
\begin{array}{cc}
3^{n}+1\,,&\text{if }n\text{ is odd}\,,\\
3^n+3\,,&\text{if }n\text{ is even}\,.
\end{array}\right.$$
A: Well, there is a simple recursion.   To get it, drop the last condition ($f(n)+f(1)\neq 1$).  We define four types of strings which pass all of your tests other than the last one.
$a_n$ are those of length $n$ in which the last equals the first (so $a_1=4$)
$b_n$ are those of length $n$ in which the last is $-1$ times the first ($b_1=0$)
$c_n$ are those of length $n$ in which the last is $i$ times the first ($c_1=0$)
$d_n$ are those of length $n$ in which the last is $-i$ times the first ($d_1=0$)
Let $T_n=a_n+b_n+c_n+d_n$  
Clearly we have $$a_n=a_{n-1}+c_{n-1}+d_{n-1}=T_{n-1}-b_{n-1}$$
$$b_n=b_{n-1}+c_{n-1}+d_{n-1}=T_{n-1}-a_{n-1}$$
$$c_n=a_{n-1}+b_{n-1}+c_{n-1}=T_{n-1}-d_{n-1}$$
$$d_n=a_{n-1}+b_{n-1}+d_{n-1}=T_{n-1}-c_{n-1}$$
It is also clear that $$T_n=3\times T_{n-1}\implies T_n=3^{n-1}\times 4$$
If, finally, we add back the final condition and let $S_n$ be the strings of length $n$ that you are actually interested in counting we easily get $$S_n=3a_{n-1}+2b_{n-1}+2c_{n-1}+2d_{n-1}=2T_{n-1}+a_{n-1}=3^{n-2}\times 8 +a_{n-1}$$
Very easy to implement this and it can be (somewhat) simplified by noting that $n≥3\implies a_n=S_{n-1}$ allowing us to write $$n≥4\implies S_n=3^{n-2}\times 8+S_{n-2}$$
If we start this off with $S_2=12,S_3=28$ we can sum the partial geometric series to get (rather ugly) closed formulas.  For even indices we get $$S_{2n}=12+9\times \left(9^{n-1}-1\right)$$ For odd indices we get $$S_{2n+1}=28+27\times \left(9^{n-1}-1\right)$$ At least for $n≥2$. Perhaps there is a more enlightening way to approach this analytically, but so far I haven't spotted it.
A: Let $A(n)$ be the number of such functions.  We define $B(n)$ as the number of functions meeting the internal requirement that have $f(n)=-f(1), C(n)$ as the number of functions meeting the internal requirement that have $f(n)=f(1), D(n)$ the number of functions meeting the internal requirement that have $f(n)=\pm i f(1)$  We can then write recurrences $$B(n)=B(n-1)+D(n-1)\\C(n)=C(n-1)+D(n-1)\\D(n)=2B(n-1)+2C(n-1)+D(n-1)\\A(n)=D(n)+C(n)\\D(n)-D(n-1)=2B(n-1)-2B(n-2)+2C(n-1)-2C(n-2)+D(n-1)-D(n-2)\\D(n)-D(n-1)=4D(n-2)+D(n-1)-D(n-2)\\D(n)=2D(n-1)+3D(n-2)\\D(2)=8, D(3)=16$$
This gives four times OEIS A122983 for $A(n)$.  With the factor $4$, it starts $12,28,84,244,732,2188,6564$  The asymptotic rate of growth is a factor $3$, as one would expect.  The end problem gets quite diluted as we go along.
A: Every function $f$ that satisfies corresponds to a function $g:\{1,\dots,n\}\to\{1,i,-i\}$ by: $$f(i+1)=f(i)g(i)\text{ for }i=1,\dots,n-1\text{ and }f(1)=f(n)g(n)$$
There is only one condition on this function $g$: $$g(1)\times\cdots\times g(n)=1$$
And function $f$ is determined by $f(1)$ and function $g$. Setting: $$g\left(k\right)=i^{r_{k}}$$ with $r_{k}\in\left\{ -1,0,1\right\} $
this condition is (caution this is ${\color{red}{wrong}}$): $$\sum_{k=1}^{n}r_{k}=0\tag1$$
So the question is now: how many of such sums exist? The answer to that is:$$\sum_{s=0}^{\lfloor\frac{1}{2}n\rfloor}\frac{n!}{s!s!\left(n-2s\right)!}$$
It must be multiplied with $4$ which is the number of possibilities for $f(1)$ so final answer:$$4\sum_{s=0}^{\lfloor\frac{1}{2}n\rfloor}\frac{n!}{s!s!\left(n-2s\right)!}$$
Unfortunately I have no closed form for this yet.

Edit (after finding mistake and deleting)
Found mistake in $(1)$: the condition is $$4\mid\sum_{k=1}^{n}r_{k}$$
(How on earth could I,...,sigh).
So we end up with:$$\sum_{4\mid s-t}\frac{n!}{\left(n-s-t\right)!s!t!}$$
Not a closed form, though. Others have served you better.
