Problem. Repeat (even) or (odd). Does every start reach ? (Open conjecture, verified far beyond .) Contest version: compute the number of steps for every start up to .

vector<int> collatzSteps(int N) {                 // steps to reach 1 for every start <= N (memoized; values may exceed N on the way)
    vector<int> s(N + 1, 0);
    for (int i = 2; i <= N; i++) {
        long long x = i; int c = 0;
        while (x >= i) { x = (x % 2) ? 3 * x + 1 : x / 2; c++; }
        s[i] = c + s[x];
    }
    return s;
}
FactValue
steps from 27111 (peak value 9232)
memoizationfollow the sequence only until it drops below the start, then reuse the table
overflowintermediate values exceed a lot: use long long
cycle detection for variants (, other maps)Floyd / Brent (Functional Graphs)