|
|
|
|
|
by rmidthun
4107 days ago
|
|
There is a way to map each rational to a specific positive integer.
First, you need to define a mapping between positives and integers, this is pretty simple. 0 1 2 3 4 0 -1 1 -2 2 in c code: if(x<0) ? (x * -2)-1 : x * 2; (pretend that's fixed width above) Next, any rational can be broken down into a unique prime factorization. 4/6 = 2^1 * 3^-1, 4/15 = 2^2 * 3^-1 * 5^-1 From the factorization, define a transform where the exponent for each prime is replaced using the mapping above:
so (2^2 * 3^-1 * 5^-1) becomes (2^4 * 3^1 * 5^1) = 240. Since the original rational can be negative, you'll need to use the first transformation again, so 4/15 = 240, -4/15 = -240, which work out to 480 and 479 respectively. 0 and 1 are left unchanged by the factorization process so 0 maps to 0, 1 to 2, and -1 to 1. |
|