-2

Consider touring the Cartesian plane spiral. The initial position is (0,0). in the first step you move to (1.0), in the second step (1.1), in step 3 you'll be in the position (0,1), step 4 takes you to (-1.1) and so on. In step 2012: At what coordinate are you arriving?, What will be the cordinate in 2121?

  • 1
    Please provide some code or ideas you have tried implementing. It's generally looked down on to use SO as a "Please do my entire assignment for me" type of thing. – AndyG Aug 15 '13 at 02:03
  • Related: [Looping in a spiral](http://stackoverflow.com/q/398299/335858) – Sergey Kalinichenko Aug 16 '13 at 01:55

1 Answers1

1

To find the nth coordinate ((0,0) is 0th), first take c=floor(sqrt(n)). If c*c==n, the coordinate is (c,c). Otherwise, if n-c*c<=2c, the coordinate is (c+c*c-n,c). Otherwise, if n-c*c<=4c, the coordinate is (-c,3c+c*c-n). Otherwise, if n-c*c<=6c, the coordinate is (n-c*c-5c,-c). Otherwise, the coordinate is (c,n-c*c-7c). Therefore, 2012 is (-29,44), and 2121 is (41,46). Please write the code yourself, this seems like homework.

hacatu
  • 638
  • 6
  • 15