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); }