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;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s