-
Notifications
You must be signed in to change notification settings - Fork 0
/
3n+1.c
40 lines (37 loc) · 778 Bytes
/
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
#include <stdio.h>
int calc_cycle_len(int num)
{
int cycle_length=1;
while(num!=1)
{
cycle_length++;
if(num%2==1)
num=num*3+1;
else
num/=2;
}
return cycle_length;
}
int main(void)
{
int temp,i,Num1,Num2,cycle_length=0,max_cycLen=0;
while(scanf("%d %d",&Num1,&Num2)!=EOF)
{
printf("%d %d ",Num1,Num2);
max_cycLen=0;
if(Num1>Num2)
{
temp=Num2;
Num2=Num1;
Num1=temp;
}
for(i=Num1;i<=Num2;i++)
{
cycle_length=calc_cycle_len(i);
if(cycle_length>max_cycLen)
max_cycLen=cycle_length;
}
printf("%d\n",max_cycLen);
}
return 0;
}