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