/home/toolbox/public_html/solutions/3/371/b.c
1
2 /*
3 * Author: Isaac Traxler
4 * Date: 2021-02-04
5 * Purpose: fun
6 * Problem: 371 - Ackermann Functions
7 */
8
9
10 #include <stdio.h>
11
12 #define DEBUG if (1 == 0)
13
14 #define TRUE (1 == 1)
15 #define FALSE (1 != 1)
16 #define MININT (-999999)
17 #define MAXSIZE 1000000
18
19 int ary[2*MAXSIZE] = {0};
20 int best;
21
22 void dump()
23 {
24 /* BEGIN FUNCTION dump */
25 int i;
26
27 printf("\n");
28 for (i=1; i < MAXSIZE; i++)
29 {
30 /* for */
31 if (0 != ary[i])
32 {
33 /* if */
34 printf("ary[%d] = (%d)\n", i, ary[i]);
35 } /* if */
36 } /* for */
37 } /* BEGIN FUNCTION dump */
38
39 int getInput(int *start, int *stop, int *s1, int *s2)
40 {
41 /* BEGIN FUNCTION getInput */
42 int moreToDo;
43
44 scanf(" %d %d ", start, stop);
45 moreToDo = ((0 < *start) && (0 < *stop));
46 if (moreToDo)
47 {
48 /* then */
49 if (*start > *stop)
50 {
51 /* then */
52 *s2 = *start;
53 *s1 = *stop;
54 } /* then */
55 else
56 {
57 /* else */
58 *s2 = *stop;
59 *s1 = *start;
60 } /* else */
61 } /* then */
62 return moreToDo;
63 } /* END FUNCTION getInput */
64
65 unsigned long int calcOne(unsigned long int in)
66 {
67 /* BEGIN FUNCTION calcOne */
68 unsigned long int retval = 1;
69 if (1 != in)
70 {
71 /* then */
72 retval = (0 == (in % 2)) ? in / 2 : 3 * in + 1 ;
73 } /* then */
74 return retval;
75 } /* END FUNCTION calcOne */
76
77 unsigned long int calc(unsigned long int in)
78 {
79 /* BEGIN FUNCTION calc */
80 unsigned long int retval;
81 int cnt = 1;
82 unsigned long int tmp;
83 unsigned long int t1;
84 int xx = 0;
85
86 DEBUG printf("Enter calc (%d)\n", in);
87 retval = in;
88 while (1 != retval)
89 {
90 /* while */
91 if (MAXSIZE > retval)
92 {
93 /* then */
94 if (0 != ary[retval])
95 {
96 /* then */
97 cnt = cnt + ary[retval] - 1;
98 } /* then */
99 else
100 {
101 /* else */
102 tmp = calcOne(retval);
103 t1 = calc(tmp);
104 cnt = cnt + t1;
105 ary[retval] = cnt - xx;
106 DEBUG printf("Array updates: ary[%d] = (%d)\n", retval, ary[retval]);
107 } /* else */
108 DEBUG printf("retval=(%d) cnt=(%d)\n", retval, cnt);
109 retval = 1;
110 } /* then */
111 else
112 {
113 /* else */
114 retval = calcOne(retval);
115 cnt++;
116 xx++;
117 DEBUG printf("retval=(%d) cnt=(%d)\n", retval, cnt);
118 } /* else */
119 } /* while */
120 return (cnt);
121 } /* BEGIN FUNCTION calc */
122
123 int process(int start, int stop)
124 {
125 /* BEGIN FUNCTION process */
126 int i;
127 int max = MININT;
128 int tmp;
129
130 if (1 == start)
131 {
132 /* range includes 1 */
133 max = 3;
134 best = 1;
135 } /* range includes 1 */
136 for (i=start; i <= stop; i++)
137 {
138 /* for */
139 tmp = calc(i) - 1;
140 if (max < tmp)
141 {
142 /* better guess */
143 best = i;
144 max = tmp;
145 } /* better guess */
146 } /* for */
147
148 return max;
149 } /* END FUNCTION process */
150
151 int main ()
152 {
153 /* main */
154 int moreToDo; /* tell me when to stop */
155 int start; /* starting point */
156 int stop; /* stopping point */
157 int s1;
158 int s2;
159 unsigned long int ans; /* calculated answer */
160
161 DEBUG init();
162 ary[1] = 1;
163 moreToDo = getInput(&start, &stop, &s1, &s2);
164 while (moreToDo)
165 {
166 /* while */
167 ans = process(s1, s2);
168 printf("Between %d and %d, %d generates the longest sequence of %d values.\n", s1, s2, best, ans);
169 moreToDo = getInput(&start, &stop, &s1, &s2);
170 } /* while */
171
172 return 1;
173 } /* main */
174
175