UVA – 100 – The 3n+1 Problem

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
using namespace std;
int i, j, a0, an, maxLoop, loop, n;
int main() {
	while (scanf("%d %d", &i, &j) == 2) {
		a0 = i, an = j, i = min(a0, an), j = max(a0, an), maxLoop = 0;
		while (i <= j && (loop = 1)) {
			n = i++;
			while (n != 1) {
				n = (n % 2) ? 3 * n + 1 : n / 2;
				loop++;
			}
			maxLoop = max(maxLoop, loop);
		}
		printf("%d %d %d\n", a0, an, maxLoop);
	}
	return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: