More naive than Raul's approach, first pass using the 'stats' lib:

   a=.2 5 7
   require'stats'
   combv=: ] {~ (comb #@])
   3 combv a
2 5 7
   2 combv a
2 5
2 7
5 7
   1 combv a
2
5
7
   combos=: (1 + i.@#) <@combv"0 1 ]
   combos a
┌─┬───┬─────┐
│2│2 5│2 5 7│
│5│2 7│     │
│7│5 7│     │
└─┴───┴─────┘
   f=: 1 : ';u/"1 each combos y'
   +f a
2 5 7 7 9 12 14
   *f a
2 5 7 10 14 35 70

/M

On Mon, Oct 2, 2017 at 10:06 AM, Raul Miller <[email protected]> wrote:
> For a first effort, I would go with
>
>   F=:1 :'(u@#~ #:@i.@(2^#))@~.'
>   f=: /F
>
> Hopefully that makes the issues obvious - the specification here calls
> for a result which grows exponentially with the size of the argument
> set.
>
> Also:
>
> The ~. might be extra work, but for typical cases the effort of
> ensuring that the argument is a set is trivial compared to the effort
> of constructing the result.
>
> You did not include the empty combination in your example results, but
> given your specification my initial inclination is to treat that as an
> oversight.
>
> I defined F instead of going straight for f because for testing
> purposes I want to be able to do (<F a), and perhaps similar things.
>
> Thanks,
>
> --
> Raul
>
>
> On Mon, Oct 2, 2017 at 12:49 PM, Skip Cave <[email protected]> wrote:
>> Given a set of integers, what is the most concise and or efficient way to
>> list the numbers along with the sum of all combinations of the numbers? the
>> products of all combinations?
>>
>> for example:
>>
>> a =. 2 5 7
>> + f a    NB. 2, 5, 7, (2+5), (2+7), (5+7), (2+5+7)
>> 2 5 7 7 9 12 14
>>
>> * f a    NB. 2, 5, 7, (2*5), (2*7), (5*7), (2*5*7)
>> 2 5 7 10 14 35 70
>>
>> The function 'f' should work for any verb and any size right argument noun
>> vector.
>>
>> Skip
>>
>> Skip Cave
>> Cave Consulting LLC
>> ----------------------------------------------------------------------
>> 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