/*
- The sequence begins with a positive integer: n
- If n is odd, the next number is: 3n+1
- If n is even, the next number is: n/2
- The sequence ends with: 1
*/
using System;
class Program
{
static void collatz_sequence(int n) {
Console.Write(n + " ");
while (n > 1) {
if (n % 2 == 0) { // even
n = n / 2;
}
else { // odd
n = 3 * n + 1;
}
Console.Write(n + " ");
}
}
static void Main() {
collatz_sequence(7);
}
}
/*
run:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
*/