Hi,

for part 1, I used the properties of the spiral pattern to find the
distance.
The spiral matrix can be viewed as a series of concentric rings with the
last number of a ring being a perfect square.
Calculating the ring number gives me one part of the distance.
Finding the distance to the nearest center of a side gives me the other
part.

   |.3 WRAP 1
37 36 35 34 33 32 31
38 17 16 15 14 13 30
39 18  5  4  3 12 29
40 19  6  1  2 11 28
41 20  7  8  *9* 10 27
42 21 22 23 24 *25* 26
43 44 45 46 47 48 *49*

Quick and almost dirty:

   dtc NB. distance to center
3 : '(<./|y - csr r)+r=.rn y'
   csr NB. side centers
3 : '((_1+t%2)+fsr y)+0 1 2 3*t=. 4%~ nsr y'
   fsr NB. first
3 : '(*y)+lsr y-1'
   nsr NB. number of elements in ring
3 : '1+(lsr - fsr) y'
   lsr NB. last
3 : '2^~1+2*y'
   rn NB. ring number
3 : '>.-:<:%:y'
   tsr NB. checking for corner cases
3 : '(''ring #'';''first'';''last'';''number'';''centers''),:(,. y);(,. fsr
y);(,. lsr y);(,. nsr y);(csr"0 y)'
   tsr (i.9),0, 300+i.2
┌──────┬──────┬──────┬──────┬───────────────────────────┐
│ring #│first │last  │number│centers                    │
├──────┼──────┼──────┼──────┼───────────────────────────┤
│  0   │     1│     1│   1  │ 0.125  0.375  0.625  0.875│
│  1   │     2│     9│   8  │     2      4      6      8│
│  2   │    10│    25│  16  │    11     15     19     23│
│  3   │    26│    49│  24  │    28     34     40     46│
│  4   │    50│    81│  32  │    53     61     69     77│
│  5   │    82│   121│  40  │    86     96    106    116│
│  6   │   122│   169│  48  │   127    139    151    163│
│  7   │   170│   225│  56  │   176    190    204    218│
│  8   │   226│   289│  64  │   233    249    265    281│
│  0   │     1│     1│   1  │ 0.125  0.375  0.625  0.875│
│300   │358802│361201│2400  │359101 359701 360301 360901│
│301   │361202│363609│2408  │361502 362104 362706 363308│
└──────┴──────┴──────┴──────┴───────────────────────────┘




On Sun, Dec 3, 2017 at 9:27 PM, Joe Bogner <[email protected]> wrote:

> Hi Jimmy - I'm also working with WRAP from the phrases. I messed around
> with trying to understand https://oeis.org/A054552 but it seemed more
> complicated than I wanted to undertake.  Here's my exploration of my
> solution for part 1
>
> I'm using this currently to locate the x/y coordinate (probably a better
> way?) within a grid
>
>  ((#~ *)@:,  ,. (I.@:*@:,)) I. (0&=)
>
>  ((#~ *)@:,  ,. (I.@:*@:,)) I. (0&=) (3 WRAP 0)
> 3 3
>
>    (3 WRAP 0)
> 42 43 44 45 46 47 48
> 41 20 21 22 23 24 25
> 40 19  6  7  8  9 26
> 39 18  5  0  1 10 27
> 38 17  4  3  2 11 28
> 37 16 15 14 13 12 29
> 36 35 34 33 32 31 30
>
>
> For my problem -- 347991 -- what's the smallest square grid to satisfy it?
>
>    %: 347991
>
> 589.908
>
>
> What argument for wrap is needed?
>
>
> -: %: 347991
>
> 294.954
>
>
> Find my target:
>
>
> ((#~ *)@:, ,. (I.@:*@:,)) I. (347991&=) (1+(295 WRAP 0))
>
> 110 590
>
>
>
>
> Try the smaller just to be sure (returns blank)
>
>    ((#~ *)@:, ,. (I.@:*@:,)) I. (347991&=) (1+(294 WRAP 0))
>
>
> Find origin (not really needed but still interesting to me)
>
>
>   ((#~ *)@:, ,. (I.@:*@:,)) I. (1&=) (1+(295 WRAP 0))
>
> 295 295
>
>
> Calc manhattan distance
>
>
>    (| (110-295)) + (|(590-295))
>
> 480
>
>
> My answer was 480
>
>
> Not particularly pretty but got to the right answer... It needs more work
> to be reusable
>
>
>
>
>
> On Sun, Dec 3, 2017 at 9:01 PM, Jimmy Gauvin <[email protected]>
> wrote:
>
> > Hi,
> >
> > has anybody found something better than a "do. while." to calculate the
> > spiral neighborhood count?
> >
> > Is there some underlying mathematical structure that I am not aware of?
> >
> >
> >
> > Thanks,
> >
> >
> >
> >
> > WRAP is from http://www.jsoftware.com/help/phrases/grids.htm
> >
> > spw is the result of the neighbor calculation
> >
> >
> > (|.2 WRAP 1){spw
> >
> > 147 142 133 122 59
> >
> > 304 5 4 2 57
> >
> > 330 10 1 1 54
> >
> > 351 11 23 25 26
> >
> > 362 747 806 880 931
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> 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