Skip to content

Instantly share code, notes, and snippets.

@kuoe0
Created January 11, 2012 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuoe0/1595194 to your computer and use it in GitHub Desktop.
Save kuoe0/1595194 to your computer and use it in GitHub Desktop.
[UVa] 100 – The 3n+1 Problem - http://kuoe0.ch/18/uva-100-the-3n-plus-1-problem/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
long long int MAX;
long long int count( int x ) {
if ( x == 1 )
return 0;
return count( x == 1 ? 3 * x + 1 : ( x >> 1 ) ) + 1;
}
int main() {
int a, b;
while ( ~scanf( "%d %d", &a, &b ) ) {
printf( "%d %d", a, b );
if ( a > b )
swap( a, b );
MAX = 0;
for ( int i = a; i < = b; ++i )
MAX = max( MAX, count( i ) + 1 );
printf( " %lldn", MAX );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment