TopCoder problem "SpiralNumbers" used in SRM 303 (Division I Level One , Division II Level Two)



Problem Statement

     Consider all positive integers written in the following manner (you can imagine an infinite spiral).
                 21  22  23  24  25  26
                 20   7   8   9  10 ...
                 19   6   1   2  11 ...
                 18   5   4   3  12 ...
                 17  16  15  14  13 ...
     
You task is to determine the position (row,column) of a given number N, assuming that the center (number 1) has position (0,0). Rows are numbered from top to bottom, columns are numbered from left to right (for example, number 3 is at (1,1)). Your method should return a string containing the position of N in form (R,C) where R is the row and C is the column. R and C must not contain any leading zeroes.
 

Definition

    
Class:SpiralNumbers
Method:getPosition
Parameters:int
Returns:String
Method signature:String getPosition(int N)
(be sure your method is public)
    
 

Constraints

- N will be between 1 and 2,147,483,647, inclusive.
 

Examples

0)
    
2
Returns: "(0,1)"
1)
    
3
Returns: "(1,1)"
2)
    
7
Returns: "(-1,-1)"
3)
    
17
Returns: "(2,-2)"
4)
    
24
Returns: "(-2,1)"
5)
    
830
Returns: "(-14,3)"
6)
    
765409
Returns: "(-437,221)"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6093

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=9824&pm=6093

Writer:

Snail

Testers:

PabloGilberto , brett1479 , Yarin , vorthys , Olexiy

Problem categories:

Simple Math, Simple Search, Iteration