Proving that $d(a,b)=p^{-n}$ is a metric for $\mathbb{Q}$ I have the following task: 

If we have the metric
  $d:\mathbb{Q}\times\mathbb{Q}\rightarrow\mathbb{R}$, so that
  $d(a,a)=0$ and $d(a,b)=p^{-n}$ always when $a-b=p^nh/k$, where
  $h,k,n\in\mathbb{Z}$ and $p$ is a prime number. Also $p$ does not
  divide $hk$. Show that $d$ is a metric for $\mathbb{Q}$.

I have all the other axioms for a metric proven, but I don't know how to prove: 
$$d(x,z)\leq d(x,y)+d(y,z)$$
for this metric. How to do this? 
 A: This is called the $p$-adic metric.
The trick to proving the triangle equality is that it is actually an ultrametric: For any $x$, $y$, $z$, two of $d(x,y)$, $d(x,z)$ and $d(y,z)$ will be equal and the third will be less than or equal to the two others.
First multiply by an appropriate integer make all three numbers integers. (This will shrink all of the distances with the same factor, depending of the number of $p$s in the common factors's prime factorization, but doesn't change the inequality).
Then write down each number in base $p$. The $p$-adic distance between two numbers now depends on the rightmost digit that differs between them, so look at the digits from right to left. If you encounter a position where the three numbers have three different digits, then $d(x,y)=d(x,z)=d(y,z)$. Otherwise, you must encounter a position where two of the numbers have the same digit, but the third -- say, $z$ -- has a different digit. In that case $d(x,z)=d(y,z)$, but $d(x,y)$ is less than this, because $x$ and $y$ agree on a longer suffix than $z$ agrees with either of them.
