Uva 100 - The 3n + 1 problem


#include <stdio.h>
inline int cycle_len(int n){
    int len = 1;
    while(n!=1)
    {
    if(n&1){
        n = (3*n+1)/2;
        len+=2;
    }
    else{
        n/=2;
        len++;
    }
    }
    return len;
}
int main(void) {
        int i, j, oi, oj, len, max;
    while(scanf("%d %d", &oi, &oj)!=EOF)
    {
        if(oi<oj){
            i=oi;
            j=oj;
        }
        else {
            i=oj;
            j=oi;
        }
        max=0;
        while(i<=j){
            len=cycle_len(i);
            if(len>max) max=len;
            i++;
        }
        printf("%d %d %d\n", oi, oj, max);
    }
        return 0;
}

Run time: 0.563

Comments

Popular Posts