It's mathematically possible to do it for finite sequences, but not very practical because the numbers required get very big very quickly: there are 677 (about 242) different length-7 sequences of integers from 1 ... 67, let alone longer sequences and larger integers.
For a simple example of such a function, map the sequence [1,6,7,8,9,45,67] to the value 21 * 36 * 57 * 78 * 119 * 1345 * 1767. The bases are the prime numbers, the powers are the elements in the sequence.
The reverse mapping is computed by division -- the number of times you can divide your value by 2
is the first element in the sequence, etc. The largest prime factor of the value tells you how long the sequence is.
If you want to allow 0
in the sequence as well as positive numbers, then add 1 to all the elements when you raise the primes to the powers. Or alternatively use the power of 2
to give the length of the sequence, then start encoding the elements starting with 3
.
Goedel used encodings like this in his proof of his Incompleteness Theorems.
As Kendall Frey says, it is not possible to define a function that maps each infinite sequence of integers to a different integer. This is a consequence of Cantor's proof that the power set of the natural numbers is uncountable: you can't even injectively map all infinite sequences of elements from {true, false}
to the integers, let alone all infinite sequences of elements from the integers.
For more practical approaches, think in terms of encoding your sequence of integers as a sequence of bytes, rather than as a number. A finite sequence of bytes can easily be considered to be a binary value, hence it's a number, you just don't really use it as such. A common representation of your example sequence is the sequence of bytes: [1,6,7,8,9,45,67]
, used for example in JSON. This is a 136-bit number. The mathematical function to reverse this mapping involves arithmetic modulo powers of 256, subtraction of the number 48, multiplication by 10, and suchlike :-)