27
TOWER OF HANOI Aisuluu Alymbekova, CSSE-124

Tower of Hanoi (and Beyond)

Embed Size (px)

Citation preview

Page 1: Tower of Hanoi (and Beyond)

TOWER OF HANOIAisuluu Alymbekova, CSSE-124

Page 2: Tower of Hanoi (and Beyond)

HISTORICAL NOTE

Historical Note

The Tower of Hanoi was invented by the French mathematician Edouard Lucas and sold as a toy in 1883. It originally bore the name of”Prof.Claus” of the college of “Li-Sou-Stain”, but these were soon discovered to be anagrams for “Prof.Lucas” of the college of “Saint Loius”, the university where he worked in Paris.

Edouard Lucas (1842-1891)

Lucas studied the Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21,… (named after the medieval mathematician, Leonardo of Pisa). Lucas may have been the first person to derive the famous formula for the nth term of this sequence involving the Golden Ratio: 1.61803… ½(1 + 5).

Lucas also has his own related sequence named after him: 2,1,3,4,7,11,… He went on to devise methods for testing the primality of large numbers and in 1876 he proved that the Mersenne number 2127 – 1 was prime. This remains the largest prime ever found without the aid of a computer.

(1180-1250)

(1 5) (1 5)

2 5

n n

n nF

2127 – 1 = 170,141,183,460,469,231,731,687,303,715,884,105,727

Lucas/Binet formula

Page 3: Tower of Hanoi (and Beyond)

START HERE - INSTRUCTIONS

1. Transfer all the disks from one pole to another pole.

2. You may move only ONE disk at a time.

3. A large disk may not rest on top of a smaller one at any time.

A B C321

Page 4: Tower of Hanoi (and Beyond)

The Tower of Hanoi

A B C

5 Tower

ILLEGAL MOVE

Page 5: Tower of Hanoi (and Beyond)

A B C

5 Tower

Page 6: Tower of Hanoi (and Beyond)

DEMO 3 TOWER

A B C

3 Tower

Page 7: Tower of Hanoi (and Beyond)

A B C

3 Tower

Page 8: Tower of Hanoi (and Beyond)

A B C

3 Tower

Page 9: Tower of Hanoi (and Beyond)

A B C

3 Tower

Page 10: Tower of Hanoi (and Beyond)

A B C

3 Tower

Page 11: Tower of Hanoi (and Beyond)

A B C

3 Tower

Page 12: Tower of Hanoi (and Beyond)

A B C

3 Tower

Page 13: Tower of Hanoi (and Beyond)

A B C

3 Tower

7 Moves

Page 14: Tower of Hanoi (and Beyond)

4 TOWER SHOW

The Tower of Hanoi

A B C

4 Tower

Page 15: Tower of Hanoi (and Beyond)

The Tower of Hanoi

A B C

4 Tower

15 Moves

Page 16: Tower of Hanoi (and Beyond)

5 TOWER SHOW

The Tower of Hanoi

A B C

5 Tower

Page 17: Tower of Hanoi (and Beyond)

The Tower of Hanoi

A B C

5 Tower

31 Moves

Page 18: Tower of Hanoi (and Beyond)

WHERE'S THE MATH IN THIS GAME?

From the moves necessary to transfer one, two, and three disks, we can find a recursive pattern - a pattern that uses information from one step to find the next step.

Unfortunately, if we want to know how many moves it will take to transfer 64 disks from post A to post C, we will first have to find the moves it takes to transfer 63 disks, 62 disks, and so on. Therefore the recursive pattern will not be much help in finding the number of moves or the time it would take to transfer all the disks.

Page 19: Tower of Hanoi (and Beyond)

The recursive pattern can help us generate more numbers to find an explicit (non-recursive) pattern. Here's how to find the number of moves needed to transfer larger numbers of disks from post A to post C, when M = the number of moves needed to transfer n-1 disks from post A to post C:

for 1 disk it takes 1 move to transfer 1 disk from post A to post C;

for 2 disks, it will take 3 moves: 2M + 1 = 2(1) + 1 = 3

for 3 disks, it will take 7 moves: 2M + 1 = 2(3) + 1 = 7

for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15

for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31

for 6 disks... ?

Page 20: Tower of Hanoi (and Beyond)

EXPLICIT PATTERN

Number of Disks Number of Moves

1 1

2 3

3 7

4 15

5 31

6 63

Page 21: Tower of Hanoi (and Beyond)

POWERS OF TWO HELP REVEAL THE PATTERN:

Number of Disks (n) Number of Moves

1 21 - 1 = 2 - 1 = 1

2 22 - 1 = 4 - 1 = 3 3 23 - 1 = 8 - 1 = 7

4 24 - 1 = 16 - 1 = 15 5 25 - 1 = 32 - 1 = 316 26 - 1 = 64 - 1 = 63

Page 22: Tower of Hanoi (and Beyond)

FASCINATING FACT

So the formula for finding the number of steps it takes to transfer n disks from post A to post C is:

2 n - 1

How long would it take at a rate of 1 disc/second?

Page 23: Tower of Hanoi (and Beyond)

Can you use your calculator and knowledge of the laws of indices to work out 264?

264 = 232 x 232

2 5 7 6 9 8 0 3 7 7 6

3 8 6 5 4 7 0 5 6 6 4 0

8 5 8 9 9 3 4 5 9 2 0 0

3 0 0 6 4 7 7 1 0 7 2 0 0 0

2 5 7 6 9 8 0 3 7 7 6 0 0 0 0

3 8 6 5 4 7 0 5 6 6 4 0 0 0 0 0

1 7 1 7 9 8 6 9 1 8 4 0 0 0 0 0 0

3 8 6 5 4 7 0 5 6 6 4 0 0 0 0 0 0 08 5 8 9 9 3 4 5 9 2 0 0 0 0 0 0 0 0

1 7 1 7 9 8 6 9 1 8 4 0 0 0 0 0 0 0 0 0

x42949672964294967296

1 8 4 4 6 7 4 4 0 7 3 7 0 9 5 5 1 6 1 6

264 – 1 = 5

Page 24: Tower of Hanoi (and Beyond)

MillionsBillions

Trillions

1 8 4 4 6 7 4 4 0 7 3 7 0 9 5 5 1 6 1 5

Moves needed to transfer all 64 discs.

How long would it take if 1 disc/second was moved?

585 000 000 000 years

The age of the Universe is currently put at between 15 and 20 000 000 000 years.

64112 1

5.85 10 years(60 60 24 365)

xx x x

Seconds in a year.

Page 25: Tower of Hanoi (and Beyond)

Simpler statement of iterative solutionAlternating between the smallest and the next-smallest disks, follow the steps for the appropriate case:

For an even number of disks:•make the legal move between pegs A and B•make the legal move between pegs A and C•make the legal move between pegs B and C•repeat until complete

For an odd number of disks:•make the legal move between pegs A and C•make the legal move between pegs A and B•make the legal move between pegs C and B•repeat until completeIn each case, a total of 2ⁿ-1 moves are made.

Page 26: Tower of Hanoi (and Beyond)

Recursive solutionA key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached. For example:•label the pegs A, B, C — these labels may move at different steps•number the discs from 1 (smallest, topmost) to n (largest, bottommost)To move n discs from peg A to peg C:1.move n−1 discs from A to B. This leaves disc n alone on peg A2.move disc n from A to C3.move n−1 discs from B to C so they sit on disc nThe above is a recursive algorithm, to carry out steps 1 and 3, apply the same algorithm again for n−1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for n = 1.

Page 27: Tower of Hanoi (and Beyond)

Блок-схема рекурсивного алгоритма решения