This comes to basically the same speed as (perhaps a little slower than) Roger's version on my machine but, since his speedup comes to a factor of 2.7 on my machine (versus the 1.7 factor he shows), your mileage may vary:
tguD=: 3 : 0
if. 0=#y. do. i.0 else.
ix=. /:y
ix/:ix++/\(1,2~:/\ix{y)*#y
end.
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
