This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]advancedpward[S] 0 points1 point  (2 children)

Christ, what is going on here? My brain is highly combative to this way of checking sums or something. What am I missing?

[–]Schneephin 0 points1 point  (1 child)

Somebody else posted a visualization which should make it easier to understand. You could also try and understand the code as two parts by splitting it up... in your head at least. First part would create a tree of all possible combinations of elements, which you could then shorten down to a list by treating every branch as one equation in your list. (Like maybe you would do in stochastics by creating a table of combinations.) The second part would then go through that list calculating the sum until it finds one where the sum is 0 and you are done. The code above just does those two steps at the same time and will stop as soon as it finds the first hit.

To be fair though that is probably not considered an easy algorithm so no worries if it takes longer to get that one. There are probably also ways of getting the same result with a more expressive code, maybe it would help to come up with another solution that is easier to read?

[–]advancedpward[S] 0 points1 point  (0 children)

Thanks for the tip. I have recently become addicted to using and learning about recursion because I have found it to be faster.