#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int n, n1, a, b, c; /* Prints "n, (a,b)" where (a,b) is the n-th ordered pair of natural numbers under the following enumeration of the ordered pairs of nat. numbers: <x,y> before <w,z> iff (x+y < w+z) or (x+y==w+z and x<w) */ if (argc != 2) { printf("\n One argument expected! Exit.\n"); return 1; } n = atoi(argv[1]); if (n < 0) { printf("\n Natural number expected! Exit.\n"); return 2; } n1 = n; if (n == 0) { a = 0; b = 0; } else { c = 1; while (c <= n) { n = n - c; c ++; } /* Now c-1 equals the # of executions of the loop's body; i.e., c-1 is the number of the diagonal that (a,b) is in. */ a = c - 1 - n; b = n; /* b is the offset of (a,b) in diagonal c-1; a+b equals c-1. */ } printf(" n = %d, (a, b) = (%d, %d)\n", n1, a, b); return 0; }