First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Wednesday, July 16, 2014

The Collatz Sequence.

Author : Jisan
Programming language : ANSI C
Difficulty : Easy
Status : Accepted
Problem number : UVA-694
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=635&mosmsg=Submission+received+with+ID+13893934

Code:

#include<stdio.h>
int main()
{
    long long int a,l,c,i,m=0,n;
    while(scanf("%lld %lld",&a,&l)==2)
    {
        if(a<0 && l<0)
            break;
        c=0; i=a; m++;
        while(a<=l)
        {
            c++;
            if(a==1)
                break;
            if(a%2==0)
                a=a/2;
            else
                a=3*a+1;
        }
        printf("Case %lld: A = %lld, limit = %lld, number of terms = %lld\n",m,i,l,c);
    }
    return 0;
}


No comments:

Post a Comment