-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3n+1.c
57 lines (48 loc) · 1.05 KB
/
3n+1.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
48
49
50
51
52
53
54
55
56
57
/* @JUDGE_ID: 1614287 100 C "3n+1 Problem" */
/* Solution to the 3n+1 problem /onlinejudge.org/
* by: Ivan Herran
* 2023-08-06 12:30*/
/* @BEGIN_OF_SOURCE_CODE */
#include <stdio.h>
#include <stdlib.h>
/*Prototypes*/
int process(int min, int max);
int main(void)
{
int min, max, cycles, remeberMin, rememberMax;
while (scanf("%d %d", &min, &max) != EOF) {
remeberMin = min;
rememberMax = max;
if (min > max) {
int temp;
temp = max;
max = min;
min = temp;
}
cycles = process(min, max);
printf("%d %d %d\n", remeberMin, rememberMax, cycles);
}
return EXIT_SUCCESS;
}
int process(int min, int max){
int steps = 1, rememberMax = max,cycles = 0;
while (rememberMax >= min) {
max = rememberMax;
steps = 1;
while (max != 1) {
if (max % 2 == 1) {
max = 3*max+1;
}else if (max % 2 == 0) {
max = max / 2;
}
steps++;
}
if (steps > cycles) {
cycles = steps;
}
steps = 1;
rememberMax--;
}
return cycles;
}
/* @END_OF_SOURCE_CODE */