Skip to content
Discussion options

You must be logged in to vote

It reads as an off-by-one bug, not a deliberate alternative convention — and the loop's own invariant proves it.

Walk the loop's contract: i counts transitions — each iteration applies one Collatz step (s → s/2 or s → 3s+1) and increments i once. When the loop exits, s has become 1, and i is the number of steps taken to get there. collatz_countdown(12) returns 9: 12→6→3→10→5→16→8→4→2→1 is nine transitions. ✓

Now apply the same contract to the already-terminal case: collatz_countdown(1) — how many steps does it take for 1 to be at 1? Zero. There are no transitions, so by the function's own semantics the answer is 0, and the early-exit branch should read:

if s == 1:
    return 0

Returning 1 …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dab00gieman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants