# Bijection from N to Q

## Main Question or Discussion Point

I am bored and feel like doing something useless today so I'm going to try to give an explicit formula that maps N to Q that is a one-to-one correspondence. If you want to waste some time, too, then feel free to post your functions or ideas towards that goal.

Something that might be very useful is a formula I found a while back for this integer sequence (a Smarandache crescendo sequence):
S={1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, ...} .
Right now, I'm thinking this formula could be used to specify the denominators of the rational numbers mapped to by the desired function. This would give us the positive rationals and then compose it with another function that will give us all rationals, not forgetting zero of course.

Of course, redundancy (like 1/1=2/2=3/3...) will have to be dealt with to ensure that the result is one-to-one.

I'll give a formula that maps N to S (where N starts at 1 in this case):
$$a_{n}=\frac{1}{2}\left( 2n+\left[ \sqrt{2n}\right] -\left[ \sqrt{2n}\right] ^{2}\right)$$
where [x] is the closest integer to x.

I haven't proved that this formula works for all n yet but I'm fairly sure it does. I (meaning Mathematica) have checked for the first 400,000 natural numbers that
$$a_{n}>1\rightarrow a_{n}=a_{n-1}+1$$
where the arrow is logical implication,
and that
$$a_{r\left( n\right) }=1$$ where $$r\left( n\right) =\frac{1}{2}\left( n^{2}-n+2\right)$$.
Incidentally, for all n>1,
$$a_{r\left( n\right) -1}=n-1$$

Related Set Theory, Logic, Probability, Statistics News on Phys.org
Preform the following steps to show:
$$|\mathbb{Q}^+ | = |\mathbb{N}|$$.

Any element in Q can be written in reduced form as m/n.

Now we exploit the fundmental theorem of arithmetic.

Either:
a)m=1 or $$m = p_1^{a_1} .... p_k^{a_k}$$.
b)n=1 or $$n=q_1^{b_1}.... q_s^{a_s}$$.

Define $$f:\mathbb{Q}^+ \mapsto \mathbb{N}$$ as follows:
1)f(1) = 1
2)$$f(m/n) = p_1^{2a_1}...p_k^{2a_k}$$ if n=1 and m!=1
3)$$f(m/n) = q_1^{2b_1-1}...q_s^{2a_s-1}$$ if n!=1 and m=1
4)$$f(m/n) = p_1^{2a_1}...p_k^{2a_k}q_1^{2b_1-1}...q_s^{2a_s-1}$$ if n!=1 and m!=1

Now show that f is a bijection.

As I said initially,
phoenixthoth said:
I'm going to try to give an explicit formula that maps N to Q.
So your map might be helpful if you can exhibit its inverse.

I think I found a map from N onto Q which satisfies me enough for now...It's pretty obvious that |N| <= |Q| via the identity map and the interesting thing would be that |N| >= |Q| via the exhibition of a map from N onto Q.

First some definitions ([x] still means the integer closest to x...the case
where x=z+1/2 for a whole number z doesn't occur in the formulas below):

$$s\left( n\right) =\frac{1}{2}\left( 2n+\left[ \sqrt{2n}\right] -\left[ \sqrt{2n}\right] ^{2}\right)$$

$$t\left( n\right) =\frac{1}{2}\left( 2-2n+\left[ \sqrt{2n}\right] +\left[ \sqrt{2n}\right] ^{2}\right)$$

$$f\left( n\right) =\frac{s\left( n\right) }{t\left( n\right) }$$

The outputs of s start with 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, ...
1, 2, 1, 3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1,... .

The intuition behind this whole idea is the array that is often used to
visualize the bijection between N and Q+; you have in the ith row and jth
column the rational number i/j and then "count" the array by going
diagonally: (1,1)-->1, (1,2)-->2, (2,1)-->3, (1,3)-->4, (2,2)-->5, (3,1)-->6, etc. Note that the pattern is ( s(n) , t(n) ) --> n.

This f seems very likely to map N onto Q+ but is definitely not injective.
To show that it is onto, first define a function from NxN to N:
$$I\left( p,q\right) =\frac{1}{2}\left( p^{2}+q^{2}+2pg-p-3q+2\right)$$

I've checked for p and q up to 200 (40,000 cases) that
$$f\left( I\left( p,q\right) \right) =p/q$$

though it will take some more work to show that this is always true. If it
is true, then the element mapped to p/q in Q+ is I(p,q).

The next idea is to map N onto the set of nonnegative rationals via g where
g(1)=0 and g(n)=f(n-1) for n>1.

Finally, N will be mapped to Q via h where
$$h\left( n\right) =\left( -1\right) ^{n}g\left( \left\lfloor n/2\right\rfloor +1\right)$$

where $$\left\lfloor x\right\rfloor$$ is the greatest integer less than or
equal to x. Thus the outputs of h look like this:
-g(1), g(2), -g(2), g(3), -g(3), ... which is the same as
0, f(1), -f(1), f(2), -f(2), ... .

Now if for some reason you just want to have s and t in the answer, then
here is (very likely to be) an onto map from N to Q:
$$1\mapsto 0$$ and for n>1,

$$n\mapsto \left( -1\right) ^{n}\frac{s\left( \left\lfloor n/2\right\rfloor \right) }{t\left( \left\lfloor n/2\right\rfloor \right) }$$ so in other words
for n>1, n gets mapped to

$$\left( -1\right) ^{n}\frac{2\left\lfloor n/2\right\rfloor +\left[ \sqrt{ 2\left\lfloor n/2\right\rfloor }\right] -\left[ \sqrt{2\left\lfloor n/2\right\rfloor }\right] ^{2}}{2-2\left\lfloor n/2\right\rfloor +\left[ \sqrt{2\left\lfloor n/2\right\rfloor }\right] +\left[ \sqrt{2\left\lfloor n/2\right\rfloor }\right] ^{2}}$$

This last map seems to have an interesting graph. I'll give some sketches for some domains of increasing magnitude:
http://img267.imageshack.us/img267/885/index51wy2.gif [Broken]
http://img411.imageshack.us/img411/5536/index53fp3.gif [Broken]
http://img267.imageshack.us/img267/8630/index55hh2.gif [Broken]
http://img528.imageshack.us/img528/7324/index57dw7.gif [Broken]

Now the same graphs but all cropped to a range of [-7,7]:
http://img517.imageshack.us/img517/1176/index64ap2.gif [Broken]
http://img517.imageshack.us/img517/6554/index66cy5.gif [Broken]
http://img267.imageshack.us/img267/1227/index68rt8.gif [Broken]
http://img517.imageshack.us/img517/7633/index70gv4.gif [Broken]
It seems like some kind of fractal...

Last edited by a moderator:
I believe I have a function from N onto Z^2... Z^2 has obvious connections to Q.

This is how it looks

And the formulas along with the mathematica code I used is [URL [Broken].[/URL]

Last edited by a moderator:
I found some simpler formulas as well as a "many-valued" inverse.