Create a universal integer sequence

Husk, 5 bytes

This prints an infinite list


Try it online! or find the first index of your sequence. (Takes a lot of memory for most sequences)


   ݱ   Infinite list [1,-1,2,-2,3,-3,4,-4,5,-5...]
  …     Rangify       [1,0,-1,0,1,2,1,0,-1,-2,...]
 Ṗ      Powerset
Σ       Concatenate

In Husk behaves nicely for infinite lists. You can see it's behaviour here

Python 2, 49 46 43 bytes

def f(n):d=len(`n`);return n/d**(n%d)%d-d/2

f(n) returns an only. This uses the smallest digit of n in base d to extract one of the higher digits.

Try it online! This script (courtesy of Dennis) takes any finite sequence and gives you an n where that sequence begins.


     (n%d)         least significant digit of n
n/d**(   )%d       get n%d-th digit of n
            -d/2   offset to get negative values

For example, for n in the range 3141592650 to 3141592659, d=10 and the last digit of n selects one of the other digits. Then we add -d/2 to get negative values.

n%d:       0  1  2  3  4  5  6  7  8  9
f(n)+d/2:  0  5  6  2  9  5  1  4  1  3
f(n):     -5  0  1 -3  4  0 -4 -1 -4 -2

Standalone alternative, also 43 bytes:

n=input();d=len(`n`);print n/d**(n%d)%d-d/2

Brachylog 2, 11 bytes


Try it online!

I also tried an algorithm using additive partitions on a list, but it wasn't any shorter. This is a generator that produces an infinite stream of integers as output; the TIO link has a header to print the first ten thousand of them.


The program starts off by trying all possible integers in sequence ( tries all remaining possibilities, for integers by default). That's 0, 1, -1, 2, -2, etc. (although the negative integers don't reach the end of the program). This is the only "infinite" step of the program; all the others are finite.

then generates all possible factorisations of the integer, treating different orders as different, and using only values from 2 upwards (so there are only finitely many); note that composite numbers are allowed in the factorisation, not just primes. This means that all possible sequences of integers ≥ 2 will be generated by this step at some point in the execution of the function (as such a sequence necessarily has some product, and that product will be generated at some point by the initial ).

We then need to map the set of those sequences onto the set of all integer sequences, which is done in two steps: subtracting 2 (-₂) from each element (), giving us the set of all nonnegative integer sequences, then taking plus or minus (, i.e. "a value whose absolute value is") each element (). The latter step is obviously nondeterministic, so Brachylog treats it as a generator, generating all possible lists whose elements are plus or minus the corresponding element of the input list. This means that we now have a generator for all possible integer sequences, and it generates them in an order that means they're all generated (specifically, the order that you get if you take the absolute value of each element, add 2 to each element, and then order by the product of the resulting elements).

Unfortunately, the question wants a single sequence, not a sequence of sequences, so we need two more commands. First, requests Brachylog to explicitly generate the sequence of sequences strictly (as opposed to producing a data structure describing the concept of a sequence generated via this method, and not actually generating the sequences until necessary); this both happens to make the program faster in this case, and ensures that the output is produced in the requested order. Finally, causes the generator to output the elements of the individual sequences one at a time (moving onto the next sequence once it's output all elements of the previous one).

The end result: each possible integer sequence gets generated and output, one element at a time, and all concatenated together into a single universal sequence.