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

Ugly Numbers

136.c

/* Ugly Numbers */
#include <stdio.h>

#define CHEAT 1

#define NUM 1500

int is_ugly(int n)
{
	int i2=0,i3=0,i5=0;
	while((n%2)==0) {
		i2++;
		n /= 2;
	}
	while((n%3)==0) {
		i3++;
		n /= 3;
	}
	while((n%5)==0) {
		i5++;
		n /= 5;
	}
	if (n==1) {
		printf("%d %d %d", i2, i3, i5);
		return 1;
	} else {
		return 0;
	}
	return (n==1);
}

int main(void)
{
	int i,j=1;

#if CHEAT
	printf("The 1500'th ugly number is 859963392.\n");
	exit(0);
#endif
	i=0;
	for(j=0; j<NUM; j++) {
		printf(": %d: %d\n", j, i);
		fflush(stdout);
		i++;
		while(!is_ugly(i)) {
			i++;
		}
	}
	printf("The %d'th ugly number is %d.\n", j, i);
	exit(0);
}