Mathematical algorithm

So, I have 49 plots in a 7x7 grid(see below) and because I am lazy, I don’t want to manually enter every adjacent block to every block. E.g Block 1’s adjacent blocks are 2,3,4,5,6,7,8,9. Is there a sequence to it, like the nth term or x = ?


You can use something like the below.
You’ll need height, width, and index:

local rightIndex = index + height
local leftIndex = index - height
local topIndex = index + 1
local bottomIndex = index - q

You simply need to know how many indexes to travel to reach the right and left adjacent block. If you count this you’ll notice that after height indexes you’ll be at the same position but on the next row.

Edit: I misread the ordering for your shape. That’s a much harder shape to find adjacents for so you may want to convert indexes into X, Y coordinates or convert them into some other ordering.

This is called Ulam Spiral, and here’s the algorithm to generate it:

1 Like

To anyone else with this problem:

After some digging the equation for the diagonal is 4x^2-2x+1

So if we take the equation for the diagonal: 1,3,13,31

x = 0: 4(0^2)-2(0) + 1 = 1
x = 1: 4(1^2)-2(1) + 1 = 3
x = 2: 4(2^2)-2(2) + 1 = 13
x = 3: 4(3^2)-2(3) + 1 = 31

Hey, this works, so now how can we find the adjacent squares?

Well to find the adjacent squares of 3 we can +1 to get 4 and -1 to get 2 to find the other 2 adjacent squares we can use the equation 4x^2-2x+1 where x=2 to get 13 then we can do the same, -1 to get 12 and +1 to get 14.

So the adjacent squares are 4, 2, 12, 14


But what if I want to find the adjacent squares of 2 for example?

Well all we need to do is slightly edit the equation, if we subtract 2-3 = -1 so our new equation is 4x^2-2x+1-1 = 4x^2-2x or if we wanted the adjacent squares of 4 we can do 4-3 = 1 so our equation is 4x^2-2x+1+1 = 4x^2-2x+2.

If you want to know more about the Ulam Spiral I highly recommend this video by numberphile. It is very interesting, especially how it was found.