Skip to content

Instantly share code, notes, and snippets.

@azmfaridee
Forked from anonymous/gist:3164292
Created July 23, 2012 16:22
Show Gist options
  • Save azmfaridee/3164541 to your computer and use it in GitHub Desktop.
Save azmfaridee/3164541 to your computer and use it in GitHub Desktop.
#include<stdio.h>
int cycle(int i){
int length = 1;
int n;
for(n = i ; n >= 1;){
if(n == 1){ break; }
if(n % 2 == 0){
n = n / 2;
length++;
} else {
n = 3*n + 1;
length++;
}
}
return length;
}
int main()
{
int a, b, max, min, i, x, max_length = 0;
while(scanf("%d %d",&a,&b) != EOF){
if(a>b){ max=a; min=b; }
else{ max=b; min=a;}
max_length = -1;
for(i = min; i <= max; i++){
x = cycle(i);
if(max_length < x)
max_length = x;
}
printf("%d %d %d\n", min, max, max_length);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment