# What is the most effective way to implement Hilbert's hotel? [duplicate]

This question already has an answer here:

Assuming I need to find an onto and 1-to-1 function from $(a,b)$ to $(0,1)$, well that's not a hard job. But things are getting bit more complicated when I'm asked to do the exact same but from $[a,b)$ to $(0,1)$ or from $(a,b)$ to $(0,1]$ and so on.

What is the most effective way to find those required functions? because I have the feeling that there is a scheme that I can work by to handle those kind of problems handling with the cardinality of the continuum, $\aleph$.

-

## marked as duplicate by Brian M. Scott, Marc van Leeuwen, Brandon Carter, Ross Millikan, Asaf KaragilaFeb 9 '13 at 23:24

You can define easily a bijection $[a,b)\leftrightarrow [0,1)$.
Now you define $0\mapsto 1/2$ and $1/n\mapsto 1/(n+1)$, for $n\geq 2$. All the other $x$ is mapped on itself.