Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Count on Cantor

264.c

/* 264 - Count on Cantor */

#include <stdio.h>
#include <math.h>

void
cantor(int n) {
	int x,y;

	x = (sqrt(8*(n-1)+1)-1)/2;
#if DEBUG
	printf("x = %d (%d/%d .. %d/%d)\n", x, 1, x+1, x+1, 1);
#endif
	y = (1+x)*x/2;

	y = n-y;

	if (x % 2) {
		printf("TERM %d IS %d/%d\n", n, y, x+2-y);
	} else {
		printf("TERM %d IS %d/%d\n", n, x+2-y, y);
	}

#if DEBUG
	printf("y = %d\n", y);
#endif
}

int
main(void) {
	int n;

	while (scanf("%d", &n)==1) {
		cantor(n);
	}
	return 0;
}