4
2
Let N be the set of nonnegative integers, i.e. {0, 1, 2, 3, ...}.
Let c0(N) be the set of all infinite sequences in N that converge to 0, i.e. { (xn) ∈ NN | xn → 0 as n → ∞}.
c0(N) can be equivalently written as the set of all finite sequences of natural numbers, such that there are no trailing zeroes. The infinite sequence of all zeroes (corresponding to a finite sequence of length 0) is an element of c0(N).
Your task is to implement a bijection in your language between N and c0(N), in as few characters as possible. This means every number in N corresponds to a unique item of c0(N), and vice versa. Do this by defining two functions: f
, going from N and c0(N); and g
, the inverse of f
. The length in bytes of these two definitions is your score.
You may use the largest internal integer and list types of your language, even if they aren't bignums or capable of handling infinite length. However, they should theoretically still be able to terminate with the right answer if the language was modified with a suitable bignum or lazy/infinite list type.
Some suggestions for ways to pack are below. Note that these aren't quite solutions yet and could need some tweaking to work; they're mostly to get the blood flowing.
f(1463616) = f(2^6 * 3^3 * 5^0 * 7^1 * 11^2) = [6, 3, 0, 1, 2]
- Factorize the number into primes and read off exponents to get the sequence.f(64971) = f(0b1111110111001011) = [6, 3, 0, 1, 2]
- Expand the number in binary and use 0 as a separator to read strings of ones, representing the sequence in unary.
While this is a code-golf and the shortest answer will win, creative and interesting solutions are encouraged and may be subject to a bounty.
You must mean “positive” — it is simpler, by the way. The integer 0 is negative. – Nicolas Barbulesco – 2014-05-04T10:47:05.907
3@NicolasBarbulesco In maths Zero is not positive or negative. That's why there is a difference between "positive" (excluding zero) and nonnegative (including zero.) Some floating point representations allow for a "postive zero and negative zero" which, while mathematically incorrect, can be convenient in programming for avoiding loss of information in certain cases. – Level River St – 2014-05-04T11:25:46.927
@steveverrill — Not “in maths”. In the maths I learnt (in France), zero is positive and negative. -0 = +0 = 0. ℤ₊ is the set of positive integers, and ℤ₋ is the set of negative integers. To exclude 0, we say strictly positive (ℤ₊*) or strictly negative (ℤ₋*). This is simple, and logical. And this makes many theorems work. In the UK, I have encountered your conception of things, and the convoluted vocabulary it needs. – Nicolas Barbulesco – 2014-05-04T15:21:07.660
1@NicolasBarbulesco: In none of the four languages I read have I ever seen this definition of positivity. While I agree that the French definition might be simpler (for monotone functions, I prefer increasing and strictly increasing to non-decreasing and increasing as well), it's certainly not commonplace. – Dennis – 2014-05-04T15:36:17.833