2

I don't know how to name this but this is what I would need.

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

This is a map of blocks. It starts by 0 and the go 1, 2, 3, 4, 5.... circular clockwise. So given that I know the number 52, in this case I would like to know it's $(x, y)$, which in this case is $(x=4,\ y=0)$.

The first thing I need is:

  • Having the number 59 for example find $x$ which $x^2$ is closer but lower to the 59. In this case is 7.

Which gives me the magnitude, to know how away from 0 is, then based on that I have the length of the square, this means how many blocks are on each side. In the case of 52, I get the number 7 based on that: $52-49+1 = 4$. With the 4 then I will make a loop until it finds the position. It will turn after 8 times.

Any help is appreciated, thanks very much!

jojeck
  • 1,283
Totty.js
  • 263

3 Answers3

2

This type of construction is known as an Ulam spiral, so you might want to search for that. It has connections to prime numbers. For example, diagonals on the spiral correspond to quadratic polynomials. The youtube channel Numberphile has a great video on this subject.

JoeyBF
  • 721
2

You work by taking it apart. I will use $n$ for the number you are looking for the position of, as you use $x,y$ as the coordinates. As you say, you first want to find $m=\lfloor \sqrt n \rfloor$, the greatest integer less than or equal to the square root of $n$, as $m^2$ is the last upper right or lower left corner before $n$

If $m$ is even, its coordinates are $(-\frac m2,-\frac m2)$. Now you need to count $n-m^2$ spaces beyond. If $n \le m^2+m+1$, it is in the column just to the left of $m^2$ and its coordinates are $(-\frac m2-1,-\frac m2+n-m^2-1)$...

If $m$ is odd, its coordinates are $(\frac {m+1}2,\frac {m+1}2-1)$...

The ...s have more reasoning like the second sentence of the second paragraph. I leave that as an exercise.

Ross Millikan
  • 374,822
  • Hey, Ross! Thanks for helping me out with your ready solution (: n=52, then m=7, m^2=49. Then n-m^2=3. n≤m^2+m+1 is true because 52<=49+7+1 <=> 52<=57. Therefore the x=-m/2-1=-7/2-1=-3.5-1=-4.5 and y=-3.5+52-49-1=-0.5-1=-1.5 || I think it's missing the rounding and inverse sign for x. About y I don't know, maybe that "-1" should not be there? And of course the rounding is missing too. I will test it. I'm writing javascript, soon I will post something. – Totty.js Jun 12 '14 at 23:10
  • Also found this: http://mathoverflow.net/questions/29038/ulam-spiral-coordinate-system and http://danpearcymaths.wordpress.com/2012/09/30/infinity-programming-in-geogebra-and-failing-miserably/ – Totty.js Jun 12 '14 at 23:12
  • You are considering the case where $m$ is odd. Yes, there are some sign changes in that case, as you go downward and left instead of up and right. – Ross Millikan Jun 12 '14 at 23:26
  • Well, going the math.sqrt floor will not work! test the number 24 for example. It will have an m of 4 instead of 3... – Totty.js Jun 12 '14 at 23:35
  • It does work if you reference from both diagonals of squares. You have a choice. I suggested you start by computing the locations of all the squares, but with different formulas for odd and even ones (note I just fixed the formula for the odds). In that case you only have to count around one or two sides of the square and getting 4 from 24 is correct. You can also reference only to odd squares, in which case you may need to count around four sides of the square. In that case you want the greatest odd number less than or equal to $\sqrt n$ – Ross Millikan Jun 12 '14 at 23:40
  • That is a bit hard to express-I would prefer to use both sets of squares. I would say $m=\lfloor \sqrt n \rfloor,$ if $m\equiv 0 \pmod 2$, decrement $m$ by $1$ – Ross Millikan Jun 12 '14 at 23:42
  • Here is a prior answer where I gave some more details: http://math.stackexchange.com/questions/617574/inverse-of-ulams-spiral/617624#617624 but he is wrapping differently, so the formulas are a bit different – Ross Millikan Jun 12 '14 at 23:45
  • Hey, Ross! I've posted another answer, check out the code (: seems like it's working well (: thanks for all your help! – Totty.js Jun 13 '14 at 00:49
0

After hard work I've been able to create a not efficient algorithm checkout here the code and comments

Totty.js
  • 263
  • If you find an answer for the standard spiral, you can just add $1$ to your $n$ and use that. The only inefficiency I see is counting through $i$ to find the square root of $n$. Presumably you don't have a square root function. If you only use it once, that may well be acceptable-even for $n=1,000,000$ you only use $1,000$ loops. If not, you could look up "integer square root"-others have had this problem. It can be very fast, but the programming is more complex. – Ross Millikan Jun 13 '14 at 02:06
  • Hey, @RossMillikan ! This uses an iterative method, therefore is quite slow for very big numbers, but this will not be a problem for my game because each block will have around 2000x2000px, therefore I don't need such a massive number and also there will be created one block at the time. And your prediction seems right "acceptable-even for n=1,000,000 you only use 1,000 loops". I've tested "getXYFromUlamSpiralIndex(1000000000000);" and still takes less than a second in my browser (: But "getXYFromUlamSpiralIndex(1000000000000000);" takes 2-3 seconds. – Totty.js Jun 13 '14 at 08:25