Pages

Like our website

Monday, September 9, 2013

100 - The 3n + 1 problem from UVa in C language

100 - The 3n + 1 problem from UVa in C language.

By rasel mahmud.


#include <stdio.h>


void swap(long int *x,long int *y)

{

    int temp;

    temp=*x;

    *x=*y;

    *y=temp;

}


int main()

{

    long int i, j, cl, mcl, n, m, p;


    for(p=0; p<100; p++){


        scanf("%ld %ld", &i, &j);


        if(i>j)

            swap(&i, &j);


        m=i, mcl=1;


        while(m<=j){

            n=m, cl=1;

            while(n != 1){

                if(n%2==1)

                    n=3*n+1;


                else n=n/2;


                cl++;

            }


            if(cl>mcl)

                mcl=cl;


            m++;

        }

        printf("%ld %ld %ld\n", i, j, mcl);

    }

        return 0;

}


No comments :

Post a Comment