I’d like to find an algorithm that, given an integer n, will return the set of arrays of positive integers that sum to n. I’ve looked around the web, and the most promising thing I could find in Runy was here. That code is:
and I must say, I have no idea what’s going on there. More importantly, it throws an error about opening a file, so my non-understanding of syntax outside of SPI makes this a problem for me. Is there a simple fix here?
But still, the code does not do what you want. It prints 7 with 5 as input. Not so familiar with Ruby, and a bit short in time to explore what exactly is happening in the until loop. I’ll try to post a (more readible) algorithm here later.
Here is a nice explanation how to implement such an algorithm, including implementations in different languages:
There is no Ruby implementation, but doing it from the description, or translating the Python version, should be straight-forward. I can do that later today if you feel it exceeds your Ruby/Python skills.
That could well be because if a parallel assignment contains more variables on the left hand side than values to assign to them on the right hand side, the excess variables are set to nil. (Such as where you had l = [[n]] and then a,i=l.pop;)
There’s no question it exceeds my Ruby/Python skills. I mean, I expect I could eventually figure it out, but I’d spend an inordinate amount of time on what amounts to a one-off for someone who only uses SPI in routine ways. If you’re able to spoonfeed me this without too much trouble, I’d greatly appreciate it. I think a lot of other people would, too, because I see solutions online in other programming languages, just not for Ruby, so I expect you’d also be greatly appreciated at GeeksforGeeks, StackOverflow, etc.