UVA 100 – The 3n + 1 problem


/*
User ID: turing_13
Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=36
*/

#include<stdio.h>

main()
{
    unsigned long int a,b,c,d,e,f,n,max;
    while(scanf(" %lu %lu", &a, &b) !=EOF)
    {
        if(a<b)
        {
            c=a;
            d=b;
        }
        else
        {
            c=b;
            d=a;
        }
        for(max=0; c<=d; c++)
        {
            n=c;
            e=1;
            while(n!=1)
            {
                if(n%2 !=0)
                    n=3*n+1;
                else
                    n/=2;
                e++;
            }
            if(e>max)
                max=e;
        }
        printf("%lu %lu %lu\n", a,b,max);
    }
    return 0;
}

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments