Example #1
0
int main(int argc, char * * argv) {

	typedef CircularBuffer< uint32_t, 4 > CIRCULAR_BUFFER;

	typedef std::pair< CIRCULAR_BUFFER, uint32_t > PAIR;
	typedef std::list< PAIR > LIST;

	LIST stack;

	CIRCULAR_BUFFER b(0);

	uint32_t i = 2,
		j = 1,

		k = 3,
		l = 0,
		m = 0;

	b = j;
	b += i;

	const uint32_t M(1000000),
		T(M * 3 + 1);

	while (true) {

		while (i > 1 && i < T) {

			j = i;
			i = (j - 1) / 3;

			if (i == 1 || i * 3 + 1 != j || i % 2 == 0) {
				i = 2 * j;

			} else {
				stack.push_back(PAIR(b, k));
			}
			
			b += i;
			k++;
		}

		for (i = 0, k -= b.SIZE; i < b.SIZE; i++, k++) {

			if (b[i] < M && l < k) {
				m = b[i];
				l = k;
			}
		}

		if (stack.empty()) {
			break;

		} else {
			stack.pop_front();

			b = stack.front().first;
			k = stack.front().second;

			j = b[b.SIZE - 1];
			i = 2 * j;

			b += i;
			k++;
		}
	}

	std::cout << m << " " << l << std::endl;
}