... HAVE U TRIED .... AT LEAST 3 TIMES ... OK U CAN SEE THIS ...

Thursday, May 15, 2014

UVA- 694 : The Collatz Sequence

//Problem  link >>http://uva.onlinejudge.org/external/6/694.html

... Simple  Adhoc problem ....

#include <bits/stdc++.h>
using namespace std  ;

int main ()
{
    long long int a , b ,cas=0 ,x ,y  ;
    while (cin>>a>>b )
    {
        if (a==-1 && b==-1) break ;
        int  cnt =0 ;
         x=a  ; y= b ;
        while (x<=y && x!=1)
        {
            if (x%2) x=x*3+1 ;
            else  x=x/2 ;cnt++ ;
             if (x==1)cnt++ ;
         
        }
        cout<<"Case "<<++cas<<": "<<"A = "<<a<<", limit = "<<b<<", number of terms = " ;
        cout<<cnt<<endl ;
    }

    return 0 ;
}

No comments:

Post a Comment