No, it's just the de Bruijn sequence.
is it though?
I'm not a number theorist, but wikipedia says:
a de Bruijn sequence of order n on a size-k alphabet A is a cyclic sequence in which every possible length-n string on A occurs exactly once as a substring
the construction is the decimal expansion of the reciprocal 1 / p of a full repetend prime p greater than 10 ^ n, but I don't think it contains each n-digit number exactly once.
a de Bruijin sequence of order n in base 10 should have length of 10 ^ n, but the decimal expansion of 1 / p has a period of p - 1.
the full repetend primes in base 10:
the obvious next step is to attempt to find the algorithm for finding the optimal (fewest symbols) number for any length and base (preferably including non-integer bases like base-ψ).
bonus points if the algorithm takes the length of unicode symbols into account.
The examples are not, but they're close (de Bruijin seqeuences would have 18 or 6 fewer bytes respectively). In contrast, superpermutations are quite different, as they don't allow for repeated elements in a given span.
i / (10^n + 1) contains every n-digit number exactly once across all of its repeating sequences, but that's never a single sequence,[1] so never a de Bruijin sequence. (1/p has a least period which divides p-1, but may be shorter.)
de Bruijin is the optimal storage (B^n + n - 1) for integral length and base.
as
(10^n + 1) \ (10^2n - 1)↩︎
The paper by Tuliani, De Bruijn sequences with efficient decoding algorithms, contains an algorithm to create a de Bruijn sequence with an algorithm for finding the position of a particular number in it.
It's all a bit silly when either the number itself or even a formatted byte array would be smaller to pass around than a fat &str pointer (regardless of lifetime).
This smells of an XY problem. What are you actually trying to achieve here: with ranges, integers, their str representations? Is your i known at compile time? If if it is, why not pass it around as a plain generic <const N: u64>, monomorphizable on-demand and instantiated only once per use; instead of polluting your binary with a bunch of static str's?
You can use something akin to this mess. In the worst case scenario, for numbers made up to 4 digits (in your ..=4000 range), you'd still only be adding 16 KB to your final executable. Yet for the love of me, I can't imagine why you'd want to use anything of the sort.
It wouldn't, but it also wouldn't contain all the sequences at least once.
There's no shorter string that contains the all the sequences contiguously at least once. The wiki phrases the de Burijn definition like so:
a de Bruijn sequence of order n on a size-k alphabet A is a cyclic sequence in which every possible length-n string on A occurs exactly once as a substring (i.e., as a contiguous subsequence). Such a sequence is denoted by B(k, n) and has length kn, which is also the number of distinct strings of length n on A. Each of these distinct strings, when taken as a substring of B(k, n), must start at a different position, because substrings starting at the same position are not distinct. Therefore, B(k, n) must have at least kn symbols. And since B(k, n) has exactly kn symbols, de Bruijn sequences are optimally short with respect to the property of containing every string of length n at least once.
However, in the context of computer memory, there is no cyclical "wrapping around". We need kn unique addresses to continguous memory of n elements. So instead choose a position L to be the largest address out of kn unique starting addresses, each address being one element after the previous. L will then be at offset kn - 1 from the first address, and is the start of a length n string, for a total length of kn + n - 1.
You can also think of this as cutting the de Bruijn cycle at an arbitrary spot, and appending a copy of the first n - 1 elements so that all length n substrings are still present (restoring substrings which crossed the cut point).
This is related to a section further down which says:
a digital door lock with a 4-digit code (each digit having 10 possibilities, from 0 to 9) would have B (10, 4) solutions, with length 10000. Therefore, only at most 10000 + 3 = 10003 (as the solutions are cyclic) presses are needed to open the lock
The 10003 button presses correspond to the contiguous sequence. In the cyclic sequence, when entering the code that starts with pressing the last element of the cycle, the last button you press is n - 1 elements into the cycle.
What are you actually trying to achieve though?
horrible idea:
- create a FUSE file system containing a single procedurally generated file containing all integers between 0 and 1^32 padded with zeroes to make indexing O(1).
- mmap the file.
- save the address and leak the allocation.
- getting a &'static str from any 32-bit integer is now ""O(1)"", just index the address and cast the pointer to a static reference.
- repeat the above with another file where all files start with
-for negative numbers.
Negative number strings cannot overlap so you get none of the space savings.
Memory mapping files spends an entire page per number.
Memory mapping files spends an entire page per number.
why would it be a page per number? if the entire file is mapped at once one time before the program starts then multiple numbers will be able to share pages.
Negative number strings cannot overlap so you get none of the space savings.
well negative numbers would need to be in their own file and thus have a second mmap, so they'd multiply all the costs by 2.