  Write ${\cal F}$ for the set of homomorphisms from $\{0,1\}^d$ to ${\bf Z}$
which send $\underline{0}$ to 0 (think of members of ${\cal F}$ as labellings
of $\{0,1\}^d$ in which adjacent strings get labels differing by exactly 1),
and ${\cal F}_i$ for those which take on exactly $i$ values. We give asymptotic
formulae for $|{\cal F}|$ and $|{\cal F}_i|$.
  In particular, we show that the probability that a uniformly chosen member
${\bf f}$ of ${\cal F}$ takes more than five values tends to 0 as $d
\rightarrow \infty$. This settles a conjecture of J. Kahn. Previously, Kahn had
shown that there is a constant $b$ such that ${\bf f}$ a.s. takes at most $b$
values. This in turn verified a conjecture of I. Benjamini {\em et al.}, that
for each $t > 0$, ${\bf f}$ a.s. takes at most $td$ values.
  Determining $|{\cal F}|$ is equivalent both to counting the number of rank
functions on the Boolean lattice $2^{[d]}$ (functions $f \colon 2^{[d]}
\longrightarrow {\bf N}$ satisfying $f(\emptyset)=0$ and $f(A) \leq f(A \cup x)
\leq f(A)+1$ for all $A \in 2^{[d]}$ and $x \in [d]$) and to counting the
number of proper 3-colourings of the discrete cube (i.e., the number of
homomorphisms from $\{0,1\}^d$ to $K_3$, the complete graph on 3 vertices).
  Our proof uses the main lemma from Kahn's proof of constant range, together
with some combinatorial approximation techniques introduced by A. Sapozhenko.
