TITLE: If $f:\mathbb N\to\mathbb N$ such that $f\big(f(x)\big)=3x$, then find $f(2013)$.
QUESTION [9 upvotes]: If $f:\mathbb N\to\mathbb N$ such that $f\big(f(x)\big)=3x$, then what is the value of $f(2013)$?

REPLY [3 votes]: In what follows, I will construct a function $f$ (which is not increasing!) that satisfies $f(f(x)) = 3x$ for $x \in \mathbb N$ and $f(x) = 4026$.
How about we split up the multiplication by 3 into a multiplication b y 2 and one by 1.5? To that end, we'd need to detect if we've already multiplied our argument by 2 and then multiply it by 1.5.
So how can we detect that?
If we are given an odd number, then clearly we need to multiply by 2 because we have not already. If we take the simple function
$$
f_1(x) = \begin{cases}
2*x & \text{if $x$ is not divisible by 2}\\
x + x/2 & \text{otherwise}
\end{cases}
$$
Then this function shows the following behaviour (output from a script):
3 * 1 = 3 == 3 = f(2) = f(f(1))
3 * 2 = 6 == 6 = f(3) = f(f(2))
3 * 3 = 9 == 9 = f(6) = f(f(3))
3 * 4 = 12 != 9 = f(6) = f(f(4))
3 * 5 = 15 == 15 = f(10) = f(f(5))
3 * 6 = 18 == 18 = f(9) = f(f(6))
3 * 7 = 21 == 21 = f(14) = f(f(7))
3 * 8 = 24 != 18 = f(12) = f(f(8))

In other words, it has the property $f_1(f_1(x)) = 3x$ if $x$ is not divisible by 4. How can we fix that? This function does a better job:
$$
f_2(x) = \begin{cases}
2*x & \text{if $x$ is not divisible by 2}\\
x + x/2 & \text{if $x$ is not divisible by 4}\\
2*x & \text{if $x$ is not divisible by 8}\\
x + x/2 & \text{otherwise}
\end{cases}
$$
where the first matching condition determines the value. This function shows the following behaviour:
3 * 1 = 3 == 3 = f(2) = f(f(1))
3 * 2 = 6 == 6 = f(3) = f(f(2))
3 * 3 = 9 == 9 = f(6) = f(f(3))
3 * 4 = 12 == 12 = f(8) = f(f(4))
3 * 5 = 15 == 15 = f(10) = f(f(5))
3 * 6 = 18 == 18 = f(9) = f(f(6))
3 * 7 = 21 == 21 = f(14) = f(f(7))
3 * 8 = 24 == 24 = f(12) = f(f(8))
3 * 9 = 27 == 27 = f(18) = f(f(9))
3 * 10 = 30 == 30 = f(15) = f(f(10))
3 * 11 = 33 == 33 = f(22) = f(f(11))
3 * 12 = 36 == 36 = f(24) = f(f(12))
3 * 13 = 39 == 39 = f(26) = f(f(13))
3 * 14 = 42 == 42 = f(21) = f(f(14))
3 * 15 = 45 == 45 = f(30) = f(f(15))
3 * 16 = 48 != 36 = f(24) = f(f(16))

In other words, it handles any $x$ that is not divisible by 16 properly. It is now straightforward to construct the function $f$.
Here's some common lisp code that implements $f$ and generates the above output.
(labels
    ((fn (x)
       (flet ((indivisible-by (x y)
                (/= 0 (mod x y))))
         (do ((k 1 (1+ k)))
             ((indivisible-by x (expt 2 k))
              (if (oddp k)
                  (* 2 x)
                  (+ x (/ x 2)))))))
     (fn-bits (x) ; Same function, more intuitive
       (flet ((count-trailing-zeroes (x)
                (do ((k 0 (1+ k)))
                    ((logbitp k x) k))))
         (if (oddp (count-trailing-zeroes x))
             (+ x (/ x 2))
             (* 2 x))))
     (g (x)
       (cond
         ((= 0 x) 0)
         ((= 0 (mod x 3)) (* 3 (g (/ x 3))))
         ((= 1 (mod x 3)) (+ x 1))
         ((= 2 (mod x 3)) (- (* 3 x) 3))))
     (h (x)
       (cond
         ((= 0 x) 0)
         ((= 0 (mod x 3)) (* 3 (h (/ x 3))))
         ((= 1 (mod x 3)) (+ (* 3 x) 3))
         ((= 2 (mod x 3)) (- x 1))))
     (f (x) (fn-bits x))) ;; Choose a function to test here
  (format t "==========================================~%")
  (format t "We have f(2013) = ~a.~%~%" (f 2013))
  (loop for x from 1 to 16 do
    (let* ((fx (f x))
           (ffx (f fx))
           (3x (* 3 x))
           (proper (= 3x ffx)))
      (format t "3 * ~a = ~a ~a ~a = f(~a) = f(f(~a))~%"
              x 3x (if proper "==" "!=") ffx fx x))))

The functions $g$ and $h$ are taken from @user2425's answer for comparison.
Update: Why does the function $f$ work as expected? Consider the input $x$ in binary representation and count the number of trailing zeroes. If I call this number $N$, what $f$ does is multiply $x$ by 2 if $N$ is even and by 1.5 if $N$ is odd.
If $N$ is odd, then $x$ is multiplied by 2 and afterwards, $N$ is even.
If $N(x)$ is even, then $N(x/2)$ is odd and so is $N(x+x/2)$. Consequently, $N$ is odd after a multiplication by $1.5.$