I have a program and I need a function that takes a coordinate as input and returns an integer corresponding to the position in Ulam's spiral. The simple (but slow) way to do this would be to generate the spiral until you finally find a coordinate that matches the input and then return how many points failed. The more elegant solution would be to write the inverse function of Ulam's spiral. I'm using something this for Ulam's spiral (n is input):
p = floor(sqrt(4 * n + 1));
q = n - floor(p * p / 4);
result = (q - floor((p + 1) / 4)) * i^(p) + floor((p + 2) / 4) * i^(p - 1);
The problem is that I can't seem to figure out what the inverse of Ulam's spiral would be. It seems like the function should have an inverse (it's a bijection, isn't it?), but I have no idea how to solve the inverse of a function with floors and powers of i.