# document.write (document.title)

 Math Help > Counting > Countable > Counting Ordered Pairs -- Calculator

In part 1 of this series, I explained how to make a one-to-one correspondence between the counting numbers and ordered pairs.  Then I provided an example, illustrating the procedure.  Here, I provide a calculator to allow you to quickly find the nth ordered pair:

# Counting Ordered Pairs, a Calculator

 > > On 11/14/00 8:29:23 PM, Andrew Critch wrote: Can ANYBODY help me find a function to find the nth element of this series? {(1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1,-1), (2,-1), (2,0), ...} It's a square spiral circling out from the origin. Any help would be greatly appreciated. Here's how you calculate it.  Start with the number, n.  Enter it here: Shell = floor((sqrt(n)+1)/2)  Leg = floor((n-(2*Shell-1)^2)/(2*Shell))  Element = (n-(2*Shell-1)^2)-2*Shell*Leg-Shell+1  xn = Leg==0? Shell: Leg==1? -Element: Leg==2? -Shell: Element yn = Leg==0? Element : Leg==1? Shell : Leg==2? -Element : - Shell (,) Or, alternatively, enter an x,y pair immediately above, and the other fields will be calculated. For more information about "floor" and the use of the question-mark in these calculations, go to the bottom of this page.

Here is a table of the first 100 ordered pairs, and below that a chart depicting the square spiral.
 n Shell Leg Element x y 1 1 0 0 1 0 2 1 0 1 1 1 3 1 1 0 0 1 4 1 1 1 -1 1 5 1 2 0 -1 0 6 1 2 1 -1 -1 7 1 3 0 0 -1 8 1 3 1 1 -1 9 2 0 -1 2 -1 10 2 0 0 2 0 11 2 0 1 2 1 12 2 0 2 2 2 13 2 1 -1 1 2 14 2 1 0 0 2 15 2 1 1 -1 2 16 2 1 2 -2 2 17 2 2 -1 -2 1 18 2 2 0 -2 0 19 2 2 1 -2 -1 20 2 2 2 -2 -2 21 2 3 -1 -1 -2 22 2 3 0 0 -2 23 2 3 1 1 -2 24 2 3 2 2 -2 25 3 0 -2 3 -2 26 3 0 -1 3 -1 27 3 0 0 3 0 28 3 0 1 3 1 29 3 0 2 3 2 30 3 0 3 3 3 31 3 1 -2 2 3 32 3 1 -1 1 3 33 3 1 0 0 3 34 3 1 1 -1 3 35 3 1 2 -2 3 36 3 1 3 -3 3 37 3 2 -2 -3 2 38 3 2 -1 -3 1 39 3 2 0 -3 0 40 3 2 1 -3 -1 41 3 2 2 -3 -2 42 3 2 3 -3 -3 43 3 3 -2 -2 -3 44 3 3 -1 -1 -3 45 3 3 0 0 -3 46 3 3 1 1 -3 47 3 3 2 2 -3 48 3 3 3 3 -3 49 4 0 -3 4 -3 50 4 0 -2 4 -2 51 4 0 -1 4 -1 52 4 0 0 4 0 53 4 0 1 4 1 54 4 0 2 4 2 55 4 0 3 4 3 56 4 0 4 4 4 57 4 1 -3 3 4 58 4 1 -2 2 4 59 4 1 -1 1 4 60 4 1 0 0 4 61 4 1 1 -1 4 62 4 1 2 -2 4 63 4 1 3 -3 4 64 4 1 4 -4 4 65 4 2 -3 -4 3 66 4 2 -2 -4 2 67 4 2 -1 -4 1 68 4 2 0 -4 0 69 4 2 1 -4 -1 70 4 2 2 -4 -2 71 4 2 3 -4 -3 72 4 2 4 -4 -4 73 4 3 -3 -3 -4 74 4 3 -2 -2 -4 75 4 3 -1 -1 -4 76 4 3 0 0 -4 77 4 3 1 1 -4 78 4 3 2 2 -4 79 4 3 3 3 -4 80 4 3 4 4 -4 81 5 0 -4 5 -4 82 5 0 -3 5 -3 83 5 0 -2 5 -2 84 5 0 -1 5 -1 85 5 0 0 5 0 86 5 0 1 5 1 87 5 0 2 5 2 88 5 0 3 5 3 89 5 0 4 5 4 90 5 0 5 5 5 91 5 1 -4 4 5 92 5 1 -3 3 5 93 5 1 -2 2 5 94 5 1 -1 1 5 95 5 1 0 0 5 96 5 1 1 -1 5 97 5 1 2 -2 5 98 5 1 3 -3 5 99 5 1 4 -4 5 100 5 1 5 -5 5

 Here is a chart showing the first 100 ordered pairs.

Notes about the calculations used by this Counting Calculator

In case you didn't know, "floor" is a function such that floor(x) returns the largest integer that is not larger than x.  Also, the notation of a?b:c is used in many programming languages, such as C and Java, and it means if condition a is true, the value is b; otherwise the value is c.  These can be nested, so that a?b:c?d:e means if a is true then the value is b; else if c is true then the value is d; otherwise the value is e.  OK, this ends today's lesson on programming.  But if you want to explore further, click "View", then click "Source" in your browser.  You can see the JavaScript program statements in the function called "Calc()" which are executed on your PC when you use this calculator.  OK, I'm really done now.  Good night.

### Related pages in this website

Read the introduction to the Square Spiral and its inverse.

The webmaster and author of this Math Help site is Graeme McRae.