-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
47 lines (43 loc) · 1019 Bytes
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
int maxLength(int num1, int num2);
int numLength(int n);
int main() {
int n1, n2;
while(scanf("%d %d", &n1, &n2) == 2) {
printf("%d %d %d\n", n1, n2, maxLength(n1, n2));
}
return 0;
}
int numLength(int n) {
int result = 1;
while (n != 1) {
result++;
if (n % 2) {
n *= 3;
n++;
} else {
n = n / 2;
}
}
return result;
}
int maxLength(int n1, int n2) {
int maximumCycleLength = 0;
if (n2 < n1) {
for (int i = n2; i <= n1; i++) {
int currLength = numLength(i);
if (currLength > maximumCycleLength) {
maximumCycleLength = currLength;
}
}
} else {
for (int i = n1; i <= n2; i++) {
int currLength = numLength(i);
if (currLength > maximumCycleLength) {
maximumCycleLength = currLength;
}
}
}
return maximumCycleLength;
}