Given values v, counts n:
  v,:n
1  2  1  2  1  2  1  2  1
20 30 40 30 40 20 40 30 40

We can sum the counts keyed by value
  v (+//.) n
180 110
  since
  v (</.) n
+--------------+-----------+
|20 40 40 40 40|30 30 20 30|
+--------------+-----------+

However my actual values for count get very large,
ie up to 2^50 or more, and I needed exact integer results
so used
  v (x:@+//.) n
180 110

but this got very slow.
However once I used the indices i of the counts
  i,v,:n
0  1  2  1  2  0  2  1  2
1  2  1  2  1  2  1  2  1
20 30 40 30 40 20 40 30 40

the more indirect derivation
  (v (</.) i)
+---------+-------+
|0 2 2 2 2|1 1 0 1|
+---------+-------+
  (v (</.) i)(x:@+/@:{)every <n
180 110

is much quicker.
Any ideas why the circumlocution is faster than
the more obvious approach?  (I don't know how
much because the slow version never finished.)

Not really important in the scheme of things -
just a day lost in a Mathschallenge solution! I hope that's not a give-away.

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

Reply via email to