# Computing the composition of a piecewise function

Question:

$$\text{Define } f:\mathbb{Z}\rightarrow\mathbb{Z}\text{ by }f(x)=\begin{cases}x+3\text{ if }x\text{ is ODD}\\ x-5\text{ if }x\text{ is EVEN}\end{cases}$$

Compute $\ f \circ f$

My attempt:

$\ (f \circ f)(x) = f(f(x))$

From here do I have to create another piecewise function and consider the cases when $f(x) = x+3$ and when $f(x) = x-5$?

• Yes (although there may be a shortcut, etc, etc). You may find you can combine pieces after. – Shuri2060 Jul 30 '17 at 20:42
• Notice that if $x$ is odd then $x+3$ is even and if $x$ is even, then $x-5$ is odd. – John Wayland Bales Jul 30 '17 at 20:42

When $x$ is odd $f(x)=x+3$, so $f(x)=x+3$ is even and $f\circ f(x)=f(f(x))=(x+3)-5=x-2$.

When $x$ is even $f(x)=x-5$, so $f(x)=x-5$ is odd and $f\circ f(x)=f(f(x))=x-5+3=x-2$.

Hence $f\circ f(x)=x-2$.

Yes, you could regard the composition as a piecewise function. $$(f\circ f)(n) = f( f(n) ) = \begin{cases} f(n) + 3 & \text{if f(n) is odd, and} \\ f(n)-5 & \text{if f(n) is even}. \\ \end{cases}$$

But notice that $f(n)$ is $n$, plus some odd integer (either $+3$ or $-5$). Thus $f(n)$ is even when $n$ is odd, and vice versa. Thus we have $$f( f(n) ) = \begin{cases} f(n) + 3 & \text{if n is even, and} \\ f(n)-5 & \text{if n is odd}. \\ \end{cases}$$ But then we know what to do with $f(n)$. When $n$ is even, $f(n) = n-5$, so $$f(n) + 3 = (n-5) + 3 = n-2.$$ Thus the first part of the piecewise definition can be rewritten to give $$f( f(n) ) = \begin{cases} n-2 & \text{if n is even, and} \\ f(n)-5 & \text{if n is odd}. \\ \end{cases}$$ Similarly, of $n$ is odd, then $f(n) = n+3$, and so $$f(n)-5 = (n+3)-5 = n-2.$$ Hence the second part of the piecewise defined composition can be rewritten, giving $$f( f(n) ) = \begin{cases} n-2 & \text{if n is even, and} \\ n-2 & \text{if n is odd}. \\ \end{cases}$$ Since we get the same thing either way, we conclude that $$(f\circ f)(n) = n-2.$$

That begin said, it is perhaps more direct to do the following: there are two cases to consider, either $n$ is even, or $n$ is odd (since all integers are either odd or even). If $n$ is even, then $$(f\circ f)(n) = f(f(n)) = f(n-5).$$ But $n-5$ is odd (even - odd = odd), so $$f(n-5) = (n-5) + 3 = n-2.$$

Similarly, if $n$ is odd, then $$(f\circ f)(n) = f(n+3) = (n+3) - 5 = n-2.$$

In either case, we conclude that $$(f\circ f)(n) = n-2.$$