Recurrence relation without modulo

80 Views Asked by At

So I have this function, $f(n)$. If $n$ is odd it equals $2$, if $n$ is even it equals $1$. For example, $f(1) = 2$ and $f(2) = 1$.

I need to find a recurrence relation for this function. However, I can't figure out at all how to "properly" find one by calling $f(n-1)$. I could only find:

$$f(n) = \mod(n,2) + 1$$

Is there a way to replace the mod operator to have a proper recurrence relation that returns the same results?

EDIT: currently trying out your answers. Please post them as $$f(n) = something.

Basically I want $$f(n) when n is greater or equal to 3.

3

There are 3 best solutions below

0
On

$$f(n+1)=f(n)+(-1)^n\quad\text{with }f(1)=2.$$

2
On

$$ f(n+1) = 3-f(n)\qquad f(1) = 2 $$

2
On

You want $f(n+2)=f(n)$, so the characteristic equation is $x^2-1=0$, which has roots $1$ and $-1$. Thus the general solution is $\alpha+\beta(-1)^n$. The initial condition tells you $f(0)=1$ and $f(1)=2$, so $$ \alpha+\beta=1 \qquad \alpha-\beta=2 $$ Therefore $\alpha=3/2$ and $\beta=-1/2$. Hence $$ f(n)=\frac{3-(-1)^n}{2} $$ Then $$ f(n+1)=\frac{3-(-1)^{n+1}}{2}=\frac{3-(-1)^n}{2}+\frac{(-1)^{n}-(-1)^{n+1}}{2} =f(n)+(-1)^n $$ and you just need to set $f(0)=1$ or, if your natural numbers start from $1$, with $f(1)=2$.

Alternatively, $$ f(n+1)=\frac{3-(-1)^{n+1}}{2}=\frac{3+(-1)^n}{2}=\frac{6-(3-(-1)^n)}{2}=3-f(n) $$