#include /* The Collatz sequence. Starting from any number N: if N is even, divide it by two if N is odd, multiply it by three and add one carry on until it reaches 1 Nobody knows if there are any numbers for which the sequence never reaches one */ // one - just print the sequence void print_collatz_from(const int n) { print(n); print(" "); if (n == 1) return; else if (n % 2 == 1) print_collatz_from(3*n+1); else print_collatz_from(n/2); } // two - find out how long the sequence is without printing it int length_collatz_from(const int n) { if (n == 1) return 1; else if (n % 2 == 1) { const int rest = length_collatz_from(3*n+1); return 1 + rest; } else { const int rest = length_collatz_from(n/2); return 1 + rest; } } // main tests both void main() { while (true) { print("\nStarting point: "); const int start = read_int(); print_collatz_from(start); print("\n length = "); print(length_collatz_from(start)); print("\n"); } }