/home/toolbox/public_html/solutions/5/583/a.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
16 #define MAX_PRIMES 10001
17 #define MAX_SIZE 50000
18
19
20 int buff[MAX_SIZE];
21 int primes[MAX_PRIMES];
22 int primeCnt;
23 int num;
24
25 /*
26 * Author: Isaac Traxler
27 * Date: 2015-08-30
28 * Purpose: fun
29 * Problem: 406
30 */
31
32 /*
33 * This template reads data until a terminating value is reached.
34 */
35
36
37 void init()
38 {
39 /* FUNCTION init */
40 int i;
41 int j;
42
43 primes[0] = 2;
44 primeCnt = 1;
45
46 /* sieve of erasthones */
47 for (i=0,j=3; i<MAX_SIZE; i++,j=j+2)
48 {
49 buff[i]=j;
50 }
51 for (i=0; (MAX_PRIMES>primeCnt)&&(MAX_SIZE>i); i++)
52 {
53 /* loop through primes */
54 if (0 != buff[i])
55 {
56 /* found a prime */
57 DEBUG printf("%d -- buff[%d] = %d\n", primeCnt, i, buff[i]);
58 primes[primeCnt++] = buff[i];
59 for (j=i+buff[i]; j<MAX_SIZE; j=j+buff[i])
60 {
61 /* mark out multiples */
62 buff[j] = 0;
63 } /* mark out multiples */
64 } /* found a prime */
65 } /* loop through primes */
66 DEBUG printf("pime cnt %d\n", primeCnt);
67 } /* FUNCTION init */
68
69 void dump()
70 {
71 /* FUNCTION dump */
72 } /* FUNCTION dump */
73
74 int getInput()
75 {
76 /* FUNCTION getInput */
77 int dataReadFlag;
78
79 scanf(" %d ", &num);
80 dataReadFlag = (0 != num);
81 return (dataReadFlag);
82 } /* FUNCTION getInput */
83
84 void process()
85 {
86 /* FUNCTION process */
87 int p = 0;
88 int firstFlag = TRUE;
89
90 /* print out initial number and equla sign */
91 printf("%d =", num);
92 /* deal with negative numbers */
93 if (0 > num)
94 {
95 /* negative number */
96 printf(" -1");
97 firstFlag = FALSE;
98 num = -1 * num;
99 } /* negative number */
100 /* loop to factor number */
101 while ((1 < num) && (p < primeCnt))
102 {
103 /* factor */
104 if (0 == (num % primes[p]))
105 {
106 /* found a factor */
107 if (firstFlag)
108 {
109 /* skip x on first case */
110 firstFlag = FALSE;
111 } /* skip x on first case */
112 else
113 {
114 /* print x */
115 printf(" x");
116 } /* print x */
117 printf(" %d", primes[p]);
118 num = num / primes[p];
119 } /* found a factor */
120 else
121 {
122 /* go to next prime */
123 p++;
124 } /* go to next prime */
125 } /* factor */
126 if (1 < num)
127 {
128 /* left with a big prime factor - write it out */
129 if (! firstFlag)
130 {
131 /* print x */
132 printf(" x");
133 } /* print x */
134 printf(" %d", num);
135 } /* left with a big prime factor - write it out */
136 printf("\n");
137 } /* FUNCTION process */
138
139 int main()
140 {
141 /* main */
142 int moreToDo;
143
144 init();
145 moreToDo = getInput();
146 while (moreToDo)
147 {
148 /* while */
149 process();
150 moreToDo = getInput();
151 } /* while */
152
153 return EXIT_SUCCESS;
154 } /* main */
155