### Instruction:
For every positive integer $n$, let $\text{mod}_5 (n)$ be the remainder obtained when $n$ is divided by 5. Define a function $f: \{0,1,2,3,\dots\} \times \{0,1,2,3,4\} \to \{0,1,2,3,4\}$ recursively as follows:

\[f(i,j) = \begin{cases}\text{mod}_5 (j+1) & \text{ if } i = 0 \text{ and } 0 \le j \le 4 \text{,}\\ f(i-1,1) & \text{ if } i \ge 1 \text{ and } j = 0 \text{, and} \\ f(i-1, f(i,j-1)) & \text{ if } i \ge 1 \text{ and } 1 \le j \le 4. \end{cases}\]What is $f(2015,2)$?

### Response:

Let's first figure out what $f$ does. Right. So $f(0,0) = \text{mod}_5(0+1) = \text{mod}_5(1) = 1$. And $f(0,1) = \text{mod}_5(1+1) = \text{mod}_5(2) = 2$. What about $f(0,2)$? $f(0,2) = \text{mod}_5 (2+1) = \text{mod}_5(3) = 3$. I think we see a pattern. Yeah. $f(0,j) = j+1$ for $0 \le j \le 4$. What happens when $i = 1$? The second line of the function definition tells us that $f(1,0) = f(0,1)$. But we know that $f(0,1) = 2$. So $f(1,0) = 2$. What does the third line of the function definition tell us? It tells us that for $1 \le j \le 4$, $f(1,j) = f(0, f(1,j-1))$. So $f(1,1) = f(0, f(1,0)) = f(0,2) = 3$. Can we guess what $f(1,2)$ is? $f(1,2) = f(0, f(1,1)) = f(0,3) = 4$. And $f(1,3) = f(0, f(1,2)) = f(0,4) = 0$. Finally, $f(1,4) = f(0, f(1,3)) = f(0,0) = 1$. What should $f(2,0)$ be? $f(2,0) = f(1,1) = 3$. What about $f(2,1)$? $f(2,1) = f(1, f(2,0)) = f(1,3) = 0$. Do you see a pattern? What about $f(2,3)$? $f(2,3) = f(1,f(2,2)) = f(1,2) = 4$ Finally, what is $f(2,4)$? $f(2,4) = f(1,f(2,3)) = f(1,4) = 1$ And $f(3,0)$ is $f(2,1) = 0$. What about $f(3,1)$? $f(3,1) = f(2,f(3,0)) = f(2,0) = 3$. $f(3,2)$ is equal to $f(2,f(3,1)) = f(2,3) = 4$. $f(3,3) = f(2,f(3,2)) = f(2,4) = 1$ Finally, $f(3,4) = f(2,f(3,3)) = f(2,1) = 0$ $f(4,1) = f(3, f(4,0)) = f(3,3) = 1$ $f(4,2) = f(3, f(4,1)) = f(3,1) = 3$ $f(4,3) = f(3,f(4,2)) = f(3,3) = 1$ Finally, $f(4,4) = f(3,f(4,3)) = f(3,1) = 3$ What about $f(5,0)$? $f(5,0) = f(4, 1) = 1$ $f(5,1) = f(4, f(5,0)) = f(4, 1) = 1$ $f(5,2) = f(4,f(5,1)) = f(4,1) = 1$ $f(5,3) = f(4, f(5,2)) = f(4, 1) = 1$ $f(5,4) = f(4, f(5,3)) = f(4, 1) = 1$ Let's look at $f(6,0)$. $f(6,0) = f(5, 1) = 1$ Right. And $f(6,1) = f(5, f(6,0)) = f(5,1) = 1$. $f(6,1) = f(5, f(6,0)) = f(5,1) = 1$ Let's skip ahead to $f(6,4)$. $f(6,2) = f(5,f(6,1)) = f(5, 1) = 1$ $f(6,3)$ is $f(5, f(6,2)) = f(5,1) = 1$. Finally, $f(6,4) = f(5,f(6,3)) = f(5, 1)= 1$. Now, $f(7,0)$ is $f(6, 1) = 1$ $f(7,1) = f(6, f(7,0)) = f(6,1) = 1$ $f(7,2) = f(6, f(7,1)) = f(6, 1) = 1$ $f(7,3) = f(6,f(7,2)) = f(6,1) = 1$ Finally, $f(7,4) = f(6,f(7,3)) = f(6, 1) = 1$. I see the pattern. Once we get to $f(6,0)$, all subsequent values of the function are 1. Right. For all $i \ge 0$ and $0 \le j \le 4$, $f(i,j) = 1$. In particular, $f(2015,2) = 1$.



