Or, after a bit of algebraic reduction:

           c =: (i:1)&(([ #.^:_1 i.@^)&# { [)
           d =: (((= 0&{::) # 1 {:: ]) c (+/ .* ; [) ])

           15 d x
        _1 _1 _1  1  1 0
        _1  1  1  1  0 0
         1 _1 _1 _1  0 1
         1  1  1 _1 _1 1

-Dan

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Roger Hui
Sent: Wednesday, October 16, 2013 7:08 PM
To: Chat Forum
Subject: Re: [Jchat] finding add, subtract, ignore combinations to reach a
target number

A shorter expression for the table c :

   c =: _1 0 1{~((#x)#3)#:i.3^#x
   c -: <:3#.^:_1 i.3^#x
1



On Wed, Oct 16, 2013 at 3:41 PM, Roger Hui <[email protected]>wrote:

>    x=: 1 2.5 3.5 10 12 30
>
>    c=: _1 0 1{~((#x)#3)#:i.3^#x
>    ] d=: (15 = c+/ .*x)#c
> _1 _1 _1  1  1 0
> _1  1  1  1  0 0
>  1 _1 _1 _1  0 1
>  1  1  1 _1 _1 1
>
>    d +/ .*x
> 15 15 15 15
>
> See also 
> http://www.jsoftware.com/jwiki/Essays/Odometer<http://www.jsoftware.co
> m/jwiki/Essays/Odometer?highlight=%28xkcd%29>
>
>
> On Wed, Oct 16, 2013 at 3:33 PM, Joe Bogner <[email protected]> wrote:
>
>> I'm working on a question that I was hoping J could help answer.
>>
>> I have a overall sum on a report and I'm trying to figure out what 
>> line items went into it. The line items can be ignored, added or
subtracted.
>>
>> In other words, given a value let's say 15, what combinations of add, 
>> subtracting or ignoring numbers of a list would result in that sum?
>>
>> I found a solution on the right rack in J
>>
>>
>> http://stackoverflow.com/questions/4632322/finding-all-possible-combi
>> nations-of-numbers-to-reach-a-given-sum
>>
>> (]#~15=+/@>)(]<@#~[:#:@i.2^#)1 5 10 2.5 2.5
>>
>>
>> That only appears to work on the add case. For example, if it worked 
>> as I desired, it would output
>>
>>
>> (]#~15=+/@>)(]<@#~[:#:@i.2^#) 10 2.5 12 3.5 1 30
>>
>> 10 3.5 2.5 -1
>>
>> Any ideas on how to approach this or any solutions handy?
>>
>> This post sounds like the right approach but I don't know how to 
>> implement
>> it:
>>
>>
>> http://stackoverflow.com/questions/10943562/find-all-the-addition-and
>> -subtraction-combinations-from-an-array-of-numbers
>>
>> My report has between 10-55 possible combinations of numbers on it 
>> depending on what the report is run on. The specific one I'm solving 
>> right now has 29
>>
>> Thanks
>> ---------------------------------------------------------------------
>> - For information about J forums see 
>> http://www.jsoftware.com/forums.htm
>>
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to