I am looking for an algorithm to iterate over all arrays of length n whose entries are integers between 0 and d and whose sum is k*d. It would be even better if there is a way to do this with built-in Julia functions and iterators. The algorithm should be non-recursive and memory efficient as I am hoping to use this for reasonable values of n.
For small values of n, d, and k, I've written down all such arrays in lexicographical ordering, but I haven't been able to come up with code for iterating through all such arrays.
Base?) is an odd restriction... Regardless, you appear to want a constrained variant ofpartitions(n, m)from Combinatorics.jl.partitionsreturns an iterator object, but you would need to instantiate the arrays to test your conditions (I think), which won't be efficient. I suspect the solution is a custom implementation ofpartitionsthat incorporates your constraints. Hopefully a dev from that package shows up, because it looks very non-trivial to me! - Colin T BowersBase. Yes, I agree it looks very similar topartitions(n,m)but with the added sum constraint. I'm not sure how to handle it. Perhaps I'll look inside the Combinatorics.jl package more to see howpartitions(n,m)is coded. - Chris Harshaw