6 apr 2009

Reflected upper triangle

Question is how to generate most efficiently the following triangle (for n=5 ):

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

Here is one solution:

rut0=: 3 : '|:(-y) [\ (i.-:y * >:y) #^:_1~ ,<:/~i.y'   NB. spaces around dyadic verbs only

Both execution time and space seem to be quadratic:

   ts'rut0 1000'
0.04063091 16785664
   ts'rut0 2000'
0.191562 67121408
   ts'rut0 3000'
0.41281229 2.6845619e8

Another solution is slower:

rut1=: 3 : '|:(-y) [\ ( * <:@:(+/\)) y ( <.@%~ <: | ) i.*:y'

   ts'rut1 1000'
0.11202145 17830016
   ts'rut0 1000'
0.066635274 16785664
   ts'rut1 2000'
0.53065204 71307392
   ts'rut0 2000'
0.19167078 67121408
   ts'rut1 3000'
1.2301193 2.852169e8
   ts'rut0 3000'
0.48619975 2.6845619e8

7 apr 2009

In private communication, Groeneveld showed his fluency in J by his superb solution:

rut287=: +/\@(# , ])@}:\@i.@-

   rnkng scores 'rut0 3000';'rut287 3000'
1 8.7909662 3.0829519 2.8514769
0         1         1         1

   (rut0 -:rut287) 3000
1

RE Boss/J-blog/RUT (last edited 2009-04-07 10:06:12 by RE Boss)