Towers of Hanoi

Hanoi

If you have to move n disks from the source peg to the destination peg, there is an elegant recursive solution:

if n = 1, just move 1 disk from the source peg to the destination peg
otherwise
move n-1 disks from the source peg to the intermediate peg
move 1 disk from the source peg to the destination peg
move n-1 disks from the intermediate peg to the destination peg

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s