/home/toolbox/public_html/solutions/1/136/c.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 #define UPPER 1505
16
17 /*
18 * Author: Isaac Traxler
19 * Date: 2014-10-29
20 * Purpose: fun
21 * Problem:136 - Ugly Numbers
22 */
23
24 /*
25 * This template reads data until a terminating value is reached.
26 */
27
28
29 int a[UPPER];
30 int last = 0;
31
32 void process()
33 {
34 /* FUNCTION process */
35 int i;
36 int p2 = 0;
37 int p3 = 0;
38 int p5 = 0;
39 int np2;
40 int np3;
41 int np5;
42
43 a[0] = 1;
44 np2 = a[p2]* 2;
45 np3 = a[p3]* 3;
46 np5 = a[p5]* 5;
47 while (last < 1499)
48 {
49 /* keep hunting */
50 while ( a[p2] * 2 <= a[last]) p2++;
51 np2 = a[p2] * 2;
52 while ( a[p3] * 3 <= a[last]) p3++;
53 np3 = a[p3] * 3;
54 while ( a[p5] * 5 <= a[last]) p5++;
55 np5 = a[p5] * 5;
56 DEBUG printf("last=%d [%d] %d %d %d\n", last, a[last], np2, np3, np5);
57 if ((np2 < np3) && (np2<np5))
58 {
59 /* next 2 is smallest */
60 last++;
61 a[last] = np2;
62 } /* next 2 is smallest */
63 else if (np3 < np5)
64 {
65 /* next 3 is smallest */
66 last++;
67 a[last] = np3;
68 } /* next 3 is smallest */
69 else
70 {
71 /* next 5 is smallest */
72 last++;
73 a[last] = np5;
74 } /* next 5 is smallest */
75 } /* keep hunting */
76 } /* FUNCTION process */
77
78 int main ()
79 {
80 /* main */
81 process();
82 printf("The 1500'th ugly number is %d.\n", a[last]);
83 return EXIT_SUCCESS;
84 } /* main */
85