Decide forwards/backwards in a looping array of numbers

I really need help with this…

Basically, I have a looping array of numbers (1,2,3,4,5)

Let’s say you are at point 4, and you want to get to point 1.

Of course you could go backwards (4,3,2,1), but it would be much faster to go (4,5,1) because the numbers loop. How would I decide to keep going forward versus going backwards to reach a number?

Any help is appreciated!

Well, as long as they’re all ordered and you know the starting number, you can just see if the difference between the starting number and the selected number is greater than the largest number → the starting number and then adding that to the bottom number → selected number. This’ll look something like such:

Finding number 4 to 3:
1st Direction: 1
2nd Direction: 4 → 5, 1 → 3 = 2 + 3 = 5

For example, 4 to 1 would be 3 in one direction or 1 in the other.
Another one is 3 to 2, which would be 1 or 5.

Bit of a scuffed answer but the theory and application is there

local function fastIter(array, position)
	if position <= #array/2 then
		print("Distance from start is shorter.")
		for index = position, 1, -1 do
			print(array[index])
		end
	elseif position > #array/2 then
		print("Distance to end is shorter.")
		for index = position, #array do
			print(array[index])
		end
	end
end

This should cover essentially what you’re asking for.