TITLE: Injectivity and surjectivity of a recursive function
QUESTION [7 upvotes]: Let $f:\mathbb{N} \to \mathbb{Q}^+$ defined as follows :
$$\begin{cases} f(0) = 0 \\ f(2n) = \dfrac{1}{f(n)+1} \\ f(2n+1) = f(n)+1 \end{cases}\,.$$
It is asked to prove the injectivity then the surjectivity of $f$.

After examining first values taken by $f$, I proved by induction that $f(2^k)=\dfrac{F_k}{F_{k+1}}$ where $(F_n)$ is the fibonacci sequence.
Also one notes that if $m$ and $n$ have different parity then we can't have $f(m) = f(n)$ since $f(2\mathbb{N}) \subset (0,1)$ and $f(2\mathbb{N}+1) \subset [1,+\infty)$.
Any ideas on how to finish the proof of injectivity and any thoughts about surjectivity are welcome.
Thanks

REPLY [4 votes]: First we show $f$ is injective . . .

Suppose otherwise.

Let $a$ be the least positive integer such that $|f^{-1}\bigl(f(a)\bigr)| > 1$.

It's easily seen that $f(n)=1$ if and only if $n=1$, hence $a > 1$.

Let $b$ be a positive integer with $b > a$ such that $f(a)=f(b)$.

If $a$ is even, then so is $b$, hence
\begin{align*}
&f(a)=f(b)\\[4pt]
\implies\;&\frac{1}{f\left({\large{\frac{a}{2}}}\right)+1}=\frac{1}{f\left({\large{\frac{b}{2}}}\right)+1}\\[4pt]
\implies\;&f\left({\small{\frac{a}{2}}}\right)=f\left({\small{\frac{b}{2}}}\right)\\[4pt]
\end{align*}
contradicting the minimality of $a$.

Similarly, if $a$ is odd, then so is $b$, hence
\begin{align*}
&f(a)=f(b)\\[4pt]
\implies\;&f\left({\small{\frac{a-1}{2}}}\right)+1=f\left({\small{\frac{b-1}{2}}}\right)+1\\[4pt]
\implies\;&f\left({\small{\frac{a-1}{2}}}\right)=f\left({\small{\frac{b-1}{2}}}\right)\\[4pt]
\end{align*}
again contradicting the minimality of $a$.

Therefore $f$ is injective.

Next we show $f$ is surjective . . .

Let $\mathbb{Z}^{+}$ denote the set of positive integers, and let $\mathbb{Q}^{+}$ denote the set of positive rational numbers.

For $x\in \mathbb{Q}^{+}$, let $w(x)=p+q$, where $x={\large{\frac{p}{q}}}$, with $p,q\in \mathbb{Z}^{+}$ and $\gcd(p,q)=1$.

Our goal is to show $ \mathbb{Q}^{+}\subseteq f(\mathbb{Z}^{+})$.

Suppose otherwise.

Choose $x\in \mathbb{Q}^{+}$ such that $w(x)$ is least among all elements of $\mathbb{Q}^{+}$ which are not elements of $f(\mathbb{Z}^{+})$.

Write $x={\large{\frac{p}{q}}}$, where $p,q\in \mathbb{Z}^{+}$ and $\gcd(p,q)=1$.

Since $f(1)=1$, it follows that $p\ne q$.

If $p < q$, then by minimality of $w(x)$, we have 
$$f(n)=\frac{q-p}{p}$$
for some positive integer $n$, hence
$$f(2n)=\frac{1}{f(n)+1}=\frac{1}{{\large{\frac{q-p}{p}}}+1}=\frac{p}{q}$$
contradiction.

Similarly, if $p > q$, then by minimality of $w(x)$, we have 
$$f(n)=\frac{p-q}{q}$$
for some positive integer $n$, hence
$$f(2n+1)=f(n)+1=\frac{p-q}{q}+1=\frac{p}{q}$$
contradiction.

It follows that $f$ is surjective.