Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ACM Q100: The 3n + 1 Problem

DZone's Guide to

ACM Q100: The 3n + 1 Problem

·
Free Resource
// description of your code here


// insert code here..
#include 
  
   

void cal(int m , int n ){

   int k ;
       int num , times=1 ;
       int MAX = 0 ;

       for( k = m ; k <= n ; k++ ){
           num = k ;
               while( num != 1){
                       if( num % 2 == 0 ) {
                               num /= 2;
                       }
                       else {
                               num = 3*num+1 ;
                       }
                       times ++ ;
       }
               if(times > MAX){
                   MAX = times ;
               }
               times = 1 ;
       }

       printf("%d\n", MAX ) ;

}

int main() {

       int i , j ;

       while((scanf( "%d%d" , &i , &j ))==2){

       printf("%d %d ", i , j ) ;

       if( i < j ) {
           cal( i , j ) ;
       }
       else{
           cal( j , i ) ;
       }
       }

       return 0;
}


  
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}