Some integer n is given. If n is odd, carry out a task of 3 n+1 and n / 2 if n is even. Finish work when n reaches 1. The number created before n reaches 1 is called the cycle length. When two numbers, I and j, try to get the maximum cycle length for all numbers between I and j. Is there any way to compare the length of the cycle without writing global variables?
#include<stdio.h>
void cycle(int num) {
int count = 1;
while (1)
{
if (num == 1)
break;
if (num % 2 == 1) {
num = 3 * num + 1;
count++;
printf("%d ", num);
}
else {
num = num / 2;
count++;
printf("%d ", num);
}
}
printf("\ncycle-length : %d\n", count);
}
void cycle_count(int num1,int num2) {
int num;
for (num = num1; num <= num2; num++)
{
cycle(num);
}
}
void main()
{
int num1, num2;
scanf("%d %d", &num1,&num2);
cycle_count(num1, num2);
return;
}