> On Dec 3, 2017, at 9:33 PM, Daniel Lyons <[email protected]> wrote: > > > >> On Dec 3, 2017, at 7:01 PM, Jimmy Gauvin <[email protected]> wrote: >> >> has anybody found something better than a "do. while." to calculate the >> spiral neighborhood count? > > > Amusingly, someone made a video about a cool trick for doing this in J, it's > about 1/3rd of y'all's Youtube presence: > > https://www.youtube.com/watch?v=dBC5vnwf6Zw&t=282s > <https://www.youtube.com/watch?v=dBC5vnwf6Zw&t=282s> > > That said, I didn't see how to solve this one even with the spiral generated > until this morning in the shower and haven't had time to work on it until now. > > My approach would basically be to generate the spiral of the right size and > then do the index-decode trick from this S.O. answer: > > https://stackoverflow.com/questions/39686977/j-coordinates-with-specific-value > > <https://stackoverflow.com/questions/39686977/j-coordinates-with-specific-value> > > Once you have the coordinates, the sum of absolute value of the x- and > y-coordinate should be the solution, but you'll have to do some kind of > offset fiddling to compensate for 0,0 being in the middle rather than the > lower-left.
This turned out to be an adequate approach for part A. I just kept increasing the iteration count until my number wound up in the spiral: a =. (|.@|: ,~ >./@, + 1 + i.@#)^:300 (1 1 $ 1) >./>./a 22801 300 iterations wasn’t quite big enough, so I made it bigger. Once I had a large enough array, I used the sum/decode trick, but subtracted the location of 1: +/ ,(| (($a) #: (I. input=,a)) - (($a) #: (I. 1=,a))) I’m not in love with this and need to study some of the other replies. For part B, I just looked up the sequence in the OEIS and found the right value and plugged it in. *shrug* :) … Now I’m curious about what Roger said, that you don’t need to generate a big spiral. Part of me thinks that you could do something like compute the polar coordinate and convert to rectangular. One of the diagonals winds up being squares of odd numbers: 1, 9, 25, 49. If your number is between (x-2)^2 and x^2, you know it’s on the ((x+1) / 2) ring; so because 45 is between 25 and 49, it’s on the 4th ring. That tells you the radius (4); all the solutions will have “carrying distance” of 3 or more. From here I got stuck trying to figure out the “angle” (I suspect knowing the distance to the next “middle of a side” is sufficient) but I have to get back to work for now so it’ll have to wait until later. -- Daniel Lyons ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
