data-structures · intermediate · ~15 min

Tower of Hanoi moves

Count the minimum moves recursively.

Challenge

Implement:

long long hanoi_moves(int n);

Return the minimum number of moves to solve the Tower of Hanoi with n disks, computed recursively. 0 <= n <= 62.

Input format

n in [0,62].

Output format

Minimum moves (2^n - 1).

Constraints

Use long long.

Starter code

#include <stddef.h>
/* Minimum number of moves to solve Tower of Hanoi with n disks (recursively). 0<=n<=62. */
long long hanoi_moves(int n){ (void)n; return 0; }

Common mistakes

Forgetting the +1 for moving the largest disk.

Edge cases to handle

0 disks need 0 moves.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.