Skip to main content

Tower of Hanoi

Definition​

The Tower of Hanoi is a classic recursive algorithm used to solve the problem of moving a stack of discs from one rod to another, following certain rules

Practice​

tower_of_hanoi(n, source, target, auxiliary):
if n == 1:
move_disk(source, target)
else:
tower_of_hanoi(n-1, source, auxiliary, target)
move_disk(source, target)
tower_of_hanoi(n-1, auxiliary, target, source)

move_disk(source, target):
print "Move disk from", source, "to", target