AFAIK no solution followed your hint (constant sum layers),
so using /: and gerund argument of (dyadic!) /. of former solutions, I
constructed ZZ:

   lr  'ZZ'
$ ([: /:@;@(+/"1 <@|.`</. ]) (#: [EMAIL PROTECTED]/))

   ZZ 5 4
 0  1  5  6
 2  4  7 13
 3  8 12 14
 9 11 15 18
10 16 17 19

It appeared that Kobchenko's elegant solution
        ($ [: /:@; [: <@|.`</. i.)
was far less efficient:

   5 ts'ZZ 500 400'
0.059869412 10989120
   5 ts'($ [: /:@; [: <@|.`</. i.)500 400'
0.31710508  34260352

   (ZZ -: ($ [: /:@; [: <@|.`</. i.))500 400
1

Probably monadic /. allows some improvement:

   M=:500 400 [EMAIL PROTECTED] 10000
   ts'(<@,{&.>~ <@$ #.&.> ([: (+/"1 </. ]) (#: [EMAIL PROTECTED]/)@$))M'
0.054501483 10992128
   ts'</.M'
0.25997343  32161280
   (</.-:(<@,{&.>~ <@$ #.&.> ([: (+/"1 </. ]) (#: [EMAIL PROTECTED]/)@$)))M
1

R.E. Boss


-----Oorspronkelijk bericht-----
Van: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Namens bill lam
Verzonden: zaterdag 25 november 2006 15:44
Aan: Programming forum
Onderwerp: [Jprogramming] zig-zag order

I found a problem that is said to an assignment for introductory C course
but it
turned to be not so trivial. It can be rephrased in J as: for a positive
integer
m find the array v or equivalently (m,m)$v so that result is a matrix with
atoms
of i.*:m filling in zig-zag order. (restricted to square matrix for time
being)

example
   4 4$0 1 5 6 2 4 7 12 3 8 11 13 9 10 14 15
0  1  5  6
2  4  7 12
3  8 11 13
9 10 14 15

I tackled the problem by noticing the sum of cell index pair for each layer
is
constant but run into trouble when need to eliminate extra pairs after
passing
the middle layer.

All suggestions welcome.

PS. zig-zag order is used in jpeg encoding http://en.wikipedia.org/wiki/JPEG
-- 
regards,
bill
----------------------------------------------------------------------
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