/* collatz.c -- explore the Collatz conjecture in C */ /* Note: For motivation, see the Wikipedia entry for the Collatz conjecture */ #include int main() { int i = 27; /* a particularly bad initial value of i */ /* if Collatz is right, this loop terminates for any initial value of i */ while (i != 1) { printf( "%d\n", i ); if ((i & 1) == 1) { /* i is odd */ i = i*3 + 1; } else { /* i is even */ i = i >> 1; /* same as i/2 */ } } printf( "%d\n", i ); return 0; }