/home/toolbox/public_html/solutions/3/371/l.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 -1
17 #define MAXSIZE 4000000
18 #define ulong long long int
19
20 ulong ary[MAXSIZE+5] = {0};
21 ulong best;
22 ulong start;
23 ulong stop;
24
25 void dump()
26 {
27 /* BEGIN FUNCTION dump */
28 ulong i;
29
30 printf("\n");
31 for (i=1; i < MAXSIZE; i++)
32 {
33 /* for */
34 if (0 != ary[i])
35 {
36 /* if */
37 printf("ary[%lld] = (%lld)\n", i, ary[i]);
38 } /* if */
39 } /* for */
40 } /* BEGIN FUNCTION dump */
41
42 int getInput()
43 {
44 /* BEGIN FUNCTION getInput */
45 int moreToDo;
46 ulong tmp;
47
48 scanf(" %lld %lld ", &start, &stop);
49 moreToDo = ((0 < start) && (0 < stop));
50 if (moreToDo)
51 {
52 /* then */
53 if (start > stop)
54 {
55 /* then */
56 tmp = start;
57 start = stop;
58 stop = tmp;
59 } /* then */
60 } /* then */
61 return moreToDo;
62 } /* END FUNCTION getInput */
63
64 ulong calcOne(ulong in)
65 {
66 /* BEGIN FUNCTION calcOne */
67 ulong retval = 1;
68 if (1 != in)
69 {
70 /* then */
71 retval = (0 == (in % 2)) ? in / 2 : 3 * in + 1 ;
72 } /* then */
73 return retval;
74 } /* END FUNCTION calcOne */
75
76 ulong calc(ulong in)
77 {
78 /* BEGIN FUNCTION calc */
79 ulong retval;
80 ulong cnt = 1;
81 ulong tmp;
82 ulong t1;
83 ulong xx = 0;
84
85 DEBUG printf("Enter calc (%lld)\n", in);
86 retval = in;
87 while (1 != retval)
88 {
89 /* while */
90 if (MAXSIZE > retval)
91 {
92 /* then */
93 if (0 != ary[retval])
94 {
95 /* then */
96 cnt = cnt + ary[retval] - 1;
97 } /* then */
98 else
99 {
100 /* else */
101 tmp = calcOne(retval);
102 t1 = calc(tmp);
103 cnt = cnt + t1;
104 ary[retval] = cnt - xx;
105 DEBUG printf("Array updates: ary[%lld] = (%lld)\n", retval, ary[retval]);
106 } /* else */
107 DEBUG printf("retval=(%lld) cnt=(%lld)\n", retval, cnt);
108 retval = 1;
109 } /* then */
110 else
111 {
112 /* else */
113 retval = calcOne(retval);
114 cnt++;
115 xx++;
116 DEBUG printf("retval=(%lld) cnt=(%lld)\n", retval, cnt);
117 } /* else */
118 } /* while */
119 return (cnt);
120 } /* BEGIN FUNCTION calc */
121
122 ulong process()
123 {
124 /* BEGIN FUNCTION process */
125 ulong i;
126 ulong max;
127 ulong tmp;
128
129 max = MININT;
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 DEBUG printf("best; %lld max: %lld\n", best, max);
147 } /* for */
148
149 return max;
150 } /* END FUNCTION process */
151
152 int main ()
153 {
154 /* main */
155 int moreToDo; /* tell me when to stop */
156 ulong ans; /* calculated answer */
157
158 DEBUG init();
159 ary[1] = 1;
160 moreToDo = getInput();
161 while (moreToDo)
162 {
163 /* while */
164 DEBUG printf("%lld - %lld\n", start, stop);
165 ans = process();
166 printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n", start, stop, best, ans);
167 moreToDo = getInput();
168 } /* while */
169
170 return 0;
171 } /* main */
172
173