There's been correspondence on this or similar matters over the years.
FWIW, I would reverse the arguments, with starting value as an optional
left argument, defaulting to a J-like zero origin.
My "spiral.ijs" script has others' attempts as well as my own slow stuff.
It includes this verb, "sp", which I attributed to Pascal Jasmin (?)
from 2015:
sp =: (,~ $ |.@/:@(+/\)@(_1&|.@(}:@(2: # >:@i.) # <:@+: $ _1: , ] ,
1: , -)))
There was a lot of correspondence on various problems in the
"Advent of Code" challenge in Dec 2017. Raul Miller came up with this, for
"Day 3: Spiral Memory"
"rspiral" counts down from a corner, so "r" might equally stand for Raul
or for reverse:
rspiral =: ,~ $ [: /: }.@(2 # >:@i.@-) +/\@# <:@+: $ (, -)@(1&,)
Both these functions take the spiral's side-length as their single
argument.
(Note that number of rings limits you to odd side-lengths.)
They're pretty nifty and space-thrifty, if not immediately transparent:
NB. time and space on spirals of similar size:
ts'120 spg 0'
0.0293389 1.3286e7
ts'120 WRAPR 0'
0.187914 2.62362e6
ts'rspiral 241'
0.00248 1.58246e6
ts'sp 241'
0.00294868 2.0983e6
I'll spare you my own code!
With apologies and acknowledgment to Raul Miller and Pascal Jasmin for
offering
their code,
Mike
On 01/02/2018 03:27, Jimmy Gauvin wrote:
Not quite as elegant as the code from
http://www.jsoftware.com/help/phrases/grids.htm :
WRAP=: wrap : (wrap@]^:[)
wrap=: RL@(,.(next+i.@#))^:4
RL=: |.@|:
next=: >:@(>./)@,
RR=: |:@|.
wrapr=: RR@(,.(next+i.@#))^:4
WRAPR=: wrapr : (wrapr@]^:[)
but faster at the expense of memory :
100 ts '120 WRAPR 1'
0.05675277 2624128
100 ts '120 spg 1'
0.01644345 14819456
100 ts '240 WRAPR 1'
0.65856677 10488448
100 ts '240 spg 1'
0.07111388 59072640
Any suggestions to reduce memory usage or "clunkynes
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm