## MMM #37: Prize 4

Alan provided a very different solution from the others and provided a Matlab program for his solution:

I wrote a solution in MATLAB - I think its only uses standard mathematical functions, but I'll clarify if needed.

inputs/outputs

(x,y): as defined in your post

z: integer value at coordinate (x,y)intermediate variables:

r0: which square 'ring' the coordinate is on. 1 is ring 0, 2-9 is ring 1, etc

z1, z2: the first and last numbers in each ring

s: which side of the square a number is on: left = 0, bottom = 1, right = 2, top = 3

l: signed distance from point to center of whichever side its on

t: angle made between point-origin line and side center-origin linefunction [x y] = ValueToCoordinates(z)

r0 = round(sqrt((z-1)/4)); % which ring is it in?

z1 = (2*r0-1).^2+1 - 2*(z==1); % lower bounding perfect odd square (+1)

z2 = (2*r0+1).^2; % upper bounding perfect odd squarel = mod(z-z1, (z2-z1+1)/4) - r0+1; % distance along side

s = floor(4*(z-z1)./(z2-z1+1)); % which side of ring?

t = atan(l./r0) ; % angle wrt origin-side perpendicularx = r0.*cos(t+s*pi/2-pi) .* abs(sec(t)); % r(t) = r0*sec(t) is polar line

y = r0.*sin(t+s*pi/2-pi) .* abs(sec(t)); % we know base radius and anglefunction z = CoordinatesToValue(x,y)

r0 = max(abs(x),abs(y)); % which ring

z1 = (2*r0-1).^2+1; % lower bounding squareT = mod(atan2(y,x),2*pi); % absolute angle of point

s = floor(mod(T - 3*pi/4-r0/1e12, 2*pi)*2/pi); % need to subtract slightly less than 3pi/4

t = mod(T - s*pi/2-pi,2*pi); % angle wrt origin-side perpendicular

l = round(tan(t).*r0); % distance along sidez = z1 + (2*s+1).*r0 - 1 + l;

## Leave a comment