[amibroker] Finding all possible combinations of numbers to reach a given sum

 

I am trying to calculate what numbers in an array or a vector added up to a particular sum. I googled it and find example of it on stackoverflow.com.
http://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum

The examples are in python and other languages. They look very simple but I have not been able to convert it to afl. I thought some smart guy on this board maybe able to convert it to afl. Much thanks in advance.

def subset_sum(numbers, target, partial=[]):
    s = sum(partial)
   
    # check if the partial sum is equals to target
    if s == target:
         print ("sum(%s)=%s" % (partial, target))
    if s >= target:
        return  # if we reach the number why bother to continue

    for i in range(len(numbers)):
        n = numbers[i]
        remaining = numbers[i+1:]
        subset_sum(remaining, target, partial + [n])
   
if __name__ == "__main__":
    subset_sum([1,2,4,8,16,32,64],104)

    #Outputs:
    #sum([1, 2])=3
    #sum([8, 32, 64])=104

 



 


__._,_.___

Posted by: l3456@excite.com
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (1)
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/


.

__,_._,___

Related Posts


EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng
:lv