How it works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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,

30th Apr 2018, 4:54 PM
Md. Nafis Ul Haque Shifat
Md. Nafis Ul Haque Shifat - avatar
1 Answer
+ 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.
30th Apr 2018, 5:24 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar