+ 6
How it works?
https://code.sololearn.com/c675tHL1VJyL/ This code shows all possible combinations of numbers to reach a given sum. I just can't understand how it works. Wishing help from anybody,
1 Réponse
+ 6
It iterates through the list items and looks for all sums of the current number and all other items in the list. If it finds it, it saves it and goes ahead, recursively, until iteration ends.
So, for the first item - 1 - it checks for 1+3, 1+5 and 1+7, then it checks for 3+5, 3+7 and finally for 5+7. Only two of them give 8, so all others are not added to the list.
Mind that since addition is reversible, there is no point in checking 5+3, since 3+5 was already checked. Same goes for any combinations with 7, as all have been checked with other numbers.