Unit 2 Task 32: Collatz hailstone length

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Unit 2 Task 32 - Collatz hailstone length

← Unit 2 tasks · Solution


The Collatz process: start with a positive integer n.

Repeat until n becomes 1. Count how many steps it took (each change is one step; you may or may not count the starting number-be consistent and state it).

Example: Starting at 6, the sequence is 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1.

Skills: while, if / else, arithmetic.