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

Repeating Decimals

202.c

#include <stdio.h>

int
main(void) {
	int num, dem;
	int i;
	int array[10000];
	int ini, len;
	int ok;

	while (1) {
		i = scanf("%d %d", &num, &dem);
		if (i != 2) {
			break;
		}
		printf("%d/%d = %d.", num, dem, num/dem);
		num %= dem;
		for (i=0; i<10000; i++) {
			num *= 10;
			array[i] = num/dem;
			num %= dem;
		}
		ok=0;
		for (ini=0; ini<5000; ini++) {
			for (len=1; len<5000; len++) {
				ok=1;
				for (i=ini+len; i<10000; i++) {
					if (array[i] != array[ini+(i-ini)%len]) {
						ok=0;
						break;
					}
				}
				if (ok) break;
			}
			if (ok) break;
		}
		for (i=0; i<50; i++) {
			if (i==ini) {
				printf("(");
			}
			if (i==ini+len) {
				printf(")");
				break;
			}
			printf("%d", array[i]);
		}
#if DEBUG
		printf("[i=%d, ini=%d, len=%d]\n", i, ini, len);
#endif
		if (i==50 && ini+len >= i) {
			printf("...)");
		}
		printf("\n   %d = number of digits in repeating cycle\n\n", len);
	}
	return 0;
}