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

 

Greetings --

I see my name mentioned in a post with a question.  Could you post some clarification, please? 

The question seems to be to find a set of component numbers that, when added together, sum to a given number.  (Not "factors" which when multiplied together result in the given number.)  Without restriction, there are many sets that will fit the requirements. 


What number are you beginning with?  Where does it come from?  What are its upper and lower bounds?  What are the restrictions on the component numbers that will be added together to reach that sum?  Is there a limit to the number of component numbers?  Are duplicates allowed?  How will they be used?


Best regards,
Howard

On Mon, Feb 15, 2016 at 3:51 PM, l3456@excite.com [amibroker] <amibroker@yahoogroups.com> wrote:
 

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: Howard B <howardbandy@gmail.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (3)
**** 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