Cleanest way to generate all combinations of n arrays

You realize this is an exponential operation in terms of both time and memory, right? It is O(K^N) where K is the base and N is the length of the number. Moreover, by asking to materialize all of these digit combinations all at once, it forces the problem to not only have exponential run time but also to allocate exponential memory. This brute force approach is only going to work for tiny toy problems.

You’d be much better off not materializing all the arrays at once. Even better still would be not materializing then at all and allowing them to be represented implicitly as integers: you can then iterate through them very quickly and ask properties of them as integers. If you want something really fast and scalable you need to avoid considering most of the combinations at all.

Hard to give more advice without knowing what you’re trying to compute.

3 Likes