I have a matrix of variable sizes, and want to find the coordinates of the n amount of squares traveled. In this example the size of the matrix is: 8. Meaning the amount of rows and columns.
Starting at point x, y: (1, 1) and;
- Going as far right as possible
- Going as far down as possible
- Going as far left as possible
- Going as far up as possible
Given that i = 53
Starting at n = 0and adding up 1to nfor each square traveled, by reaching n == i calculate the coordinates.
What I have tried:
(Disclaimer: This is mathematically unrelated, I'm only posting this so you can see how naive I am)
In my original programming problem I have achieved it via creating a variable mwith the size of the matrix so in this case m = 8, and deducing it after each rightand leftmovements.
y = 0 //y will move first, starts offboard at imaginary column `0`
x = 1 //start from row 1
m = 8
right: { y+= i; m -= 1;} //x = 1, y = 8 / m = 7
down: { x+= m; }//x = 8, y = 8
left: { y-= m; m -= 1; } //x = 8, y = 1 / m = 6
up: { x-=m; } // x = 2, y = 1
And iterating, until another created variable sumofsteps, to sum up the amount of m squares traveled reach i which is in this case 53.''
This is, of course very naive, since in some cases the matrix can be as big as 1073741824 and the number of steps 1152921504603393520, which in my program takes too long to solve.
Back to reality:
What is a mathematical formula to find the coordinates after n amount of squares traveled?
