|
|
Line 1: |
Line 1: |
| [[Image:Tower of Hanoi.jpeg|300px|thumb|A model set of the Towers of Hanoi (with 8 disks)]]
| | == being stuck feet of Xiao Yan == |
| [[Image:Tower of Hanoi 4.gif|300px|thumb|An animated solution of the '''Tower of Hanoi''' puzzle for ''T(4,3)''.]]
| |
| [[File:UniversumUNAM34.JPG|thumb|300px|Tower of Hanoi interactive display at the [[Universum (UNAM)|Universum museum]] in Mexico City]]
| |
| The '''Tower of Hanoi''' (also called the '''Tower of Brahma''' or '''Lucas' Tower''',<ref>{{cite book |last=Hofstadter |first=Douglas R. |title=Metamagical Themas : Questing for the Essence of Mind and Pattern |year=1985 |publisher=Basic Books |location=New York |isbn=0-465-04540-5}}</ref> and sometimes pluralised) is a [[mathematical game]] or [[puzzle]]. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
| |
|
| |
|
| The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
| | He anyone is [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-2.html カシオの時計] pretty good, the body is almost vindictive and open your heart and quickly spread to the toe area, a heavy riding again, finally freed from the shackles of the feet, but also the two stakes that come close escape.<br><br>although [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-6.html 電波腕時計 カシオ] dodged the attack, but when [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-2.html カシオ腕時計 g-shock] the stakes once again opt for the five hit to them, being stuck feet of Xiao Yan, [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-6.html casio 電波時計] finally, was severely hit out.<br><br>laugh, 'Yin' 'Yin' is looking at the ground groan 'Yin' Xiao Yan, 'medicine' old smiled and said: 'How?'<br><br>'You do what ghost at the stake?' Xiao Yan 'knead' the 'rub' made up of the chest, the hum of the road.<br><br>'the stakes are [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-3.html カシオ 電波時計 腕時計] painted plastic ink every time you move, you must stick with a grudge to defuse that kind of power, otherwise, once hiding behind, knocked appearances is ending, so evading the same time, you the body of a grudge, must always be kept with the moment of transfer of state, but if |
| | 相关的主题文章: |
| | <ul> |
| | |
| | <li>[http://www.ziewang.com/forum.php?mod=viewthread&tid=1511946 http://www.ziewang.com/forum.php?mod=viewthread&tid=1511946]</li> |
| | |
| | <li>[http://www.msc66n.com/forum.php?mod=viewthread&tid=799306 http://www.msc66n.com/forum.php?mod=viewthread&tid=799306]</li> |
| | |
| | <li>[http://www.gangasimports.com/cgi-bin/commerce.cgi http://www.gangasimports.com/cgi-bin/commerce.cgi]</li> |
| | |
| | </ul> |
|
| |
|
| # Only one disk can be moved at a time.
| | == '' Laugh == |
| # Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
| |
| # No disk may be placed on top of a smaller disk.
| |
|
| |
|
| With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2<sup>''n''</sup> - 1, where ''n'' is the number of disks.
| | Laugh! '' Laugh! Laugh! '' Laugh! Laugh! '' Laugh! Laugh! '' Laugh! Laugh! '<br><br>fire plume filled the sky, and in the whole body when they enter XiaoYan twenty [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-14.html カシオ腕時計 メンズ] feet away, a [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-11.html 腕時計 メンズ casio] circle of deep blue 'color' flame cover, suddenly emerged out of strange that those who [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-8.html 時計 メンズ カシオ] 'shot' [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-2.html カシオ 時計 プロトレック] into this circle of fire flames cover Yu, the diffuse gray brown above it 'color' flame, suddenly met as ice as quickly extinguished, and as the flame goes out, then into feather 'hair' vindictive, but also quietly decomposition, and finally disappear into nothingness<br>outside<br>flame cover, like a storm-like plume of fire pouring down, and the cover of the flame, Xiao Yan slowly lost his hands behind him, his eyes dull watching the sky above the huge gray brown 'color' Fire Phoenix, This is almost three body embodies the most vindictive dialect transformed condensed fire phoenix, the temperature really high terror, this degree of temperature, [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-11.html カシオ 電波時計] has been extremely close Firelight Qinglian, but repeated again and again how close |
| | 相关的主题文章: |
| | <ul> |
| | |
| | <li>[http://115.239.196.25/mtxdz/home.php?mod=space&uid=108643 http://115.239.196.25/mtxdz/home.php?mod=space&uid=108643]</li> |
| | |
| | <li>[http://www.tricolore.net/cgi-bin/personal/italians.cgi http://www.tricolore.net/cgi-bin/personal/italians.cgi]</li> |
| | |
| | <li>[http://kampfgeist.clan.me/index.php?page=Thread&threadID=138635 http://kampfgeist.clan.me/index.php?page=Thread&threadID=138635]</li> |
| | |
| | </ul> |
|
| |
|
| == Origins == | | == and in her so abnormal == |
|
| |
|
| The puzzle was first publicized in [[Western world|the West]] by the [[French people|French]] [[mathematician]] [[Édouard Lucas]] in 1883. There is a story about an [[India]]n temple in [[Kashi Vishwanath Temple|Kashi Vishwanath]] which contains a large room with three time-worn posts in it surrounded by 64 golden disks. [[Brahmin]] priests, acting out the command of an ancient prophecy, have been moving these disks, in accordance with the immutable rules of the Brahma, since that time. The puzzle is therefore also known as the Tower of [[Brahma]] puzzle. According to the legend, when the last move of the puzzle will be completed, the world will end.<ref>{{cite book |last=Spitznagel |first=Edward L. |title=Selected topics in mathematics |year=1971 |publisher=Holt, Rinehart and Winston |page=137 |isbn=0-03-084693-5}}</ref> It is not clear whether Lucas invented this legend or was inspired by it.
| | Inflammation sight, that tall Jiaoqu and representatives 'of' slender rounded legs, not Xiao Yu also is who?<br>Xiao Yu<br>Qiaode open the door [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-10.html カシオ 腕時計 スタンダード] in front of large groups of [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-6.html 電波腕時計 カシオ] people, but also lengleleng, but when look to Xiao Yan face when, suddenly hesitated, Meimou among the crystal gradually build up, like a big storm rolled like the potential.<br><br>'Do not cry, do not cry, how exactly? nonstop rush I'm out.' Since [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-15.html カシオ 腕時計 gps] the beginning of the first time I saw there in front of him, Xiao Yu 'Lucy' out such a weak stance, Xiao Yan is also great sense of shock, immediately hastily.<br><br>white teeth biting lips, Xiao Yu did finally cry out, but single-handedly took him, hurried and ran toward the house, and in her so abnormal, Xiao Yan is not the slightest resistance, allowing She pulled forward.<br><br>under the [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-9.html 電波時計 casio] leadership of Xiao Yu, a pedestrian across the hall, and finally to a place in front of the former That slowed down, [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-13.html カシオ腕時計 メンズ] gently push |
| | 相关的主题文章: |
| | <ul> |
| | |
| | <li>[http://hero-hk.freebbs.com.tw/viewthread.php?tid=319658&extra= http://hero-hk.freebbs.com.tw/viewthread.php?tid=319658&extra=]</li> |
| | |
| | <li>[http://www.366-1.com/home.php?mod=space&uid=0 http://www.366-1.com/home.php?mod=space&uid=0]</li> |
| | |
| | <li>[http://58mind.com/mh3/forum.php?mod=viewthread&tid=92481 http://58mind.com/mh3/forum.php?mod=viewthread&tid=92481]</li> |
| | |
| | </ul> |
|
| |
|
| If the legend were true, and if the priests were able to move disks at a rate of one per second, using the smallest number of moves, it would take them 2<sup>64</sup>−1 seconds or roughly 585 [[1,000,000,000 (number)|billion]] years<ref>{{cite book |last=Moscovich |first=Ivan |authorlink=Ivan Moscovich |title=1000 playthinks: puzzles, paradoxes, illusions & games |publisher=Workman |year=2001 |isbn=0-7611-1826-8 }}</ref> or 18,446,744,073,709,551,615 turns to finish, or about 127 times the current age of the sun.
| | == 'Xiao Yan brother == |
|
| |
|
| There are many variations on this legend. For instance, in some tellings, the temple is a [[monastery]] and the priests are [[monk]]s. The temple or monastery may be said to be in different parts of the world — including [[Hanoi]], [[Vietnam]], and may be associated with any [[religion]]. In some versions, other elements are introduced, such as the fact that the tower was created at the beginning of the world, or that the priests or monks may make only one move per day.
| | Illusion, for Bodhi trees, they have [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-13.html casio 腕時計 edifice] fear to the extreme, [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-4.html casio 腕時計 g-shock] there is nothing better than a dead even more miserable and sad at the illusion of being.<br><br>'Xiao Yan brother?'<br><br>Kaoru children and other people's eyes is solidified in that [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-13.html カシオ アナログ 腕時計] Bodhi trees, but then [http://www.ispsc.edu.ph/nav/japandi/casio-rakuten-14.html カシオ 腕時計 ソーラー 電波] she saw that is located on that road within the tree slim figure, the current upheaval cheek is up.<br><br>'Xiao Yan?'<br><br>Kaoru children in the face of 'color' changes, the rest of the people also saw this scene, the moment their face 'colors' are changing up.<br><br>'Kaoru children, do not impulse! Perhaps this is his big chance!' Gu Qingyang grabbed Kaoru children, Chen Sheng said.<br><br>hear the words of the ancient Qingyang, Kaoru children this awake, some good memories and records relating to this Bodhi trees, after a [http://www.nnyagdev.org/sitemap.xml http://www.nnyagdev.org/sitemap.xml] moment, this slowly nodded his head.<br><br>According to some historical records, |
| | | 相关的主题文章: |
| == Solution ==
| | <ul> |
| The puzzle can be played with any number of disks, although many toy versions have around seven to nine of them. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2<sup>''n''</sup> - 1, where ''n'' is the number of disks.<ref>{{cite book |last=Petković |first=Miodrag |title=Famous Puzzles of Great Mathematicians |year=2009 |publisher=AMS Bookstore |isbn=0-8218-4814-3 |page=197}}</ref>
| | |
| | | <li>[http://cgi.www5e.biglobe.ne.jp/~sakuno/aska/aska.cgi http://cgi.www5e.biglobe.ne.jp/~sakuno/aska/aska.cgi]</li> |
| === Iterative solution ===
| | |
| A simple solution for the toy puzzle: Alternate moves between the smallest piece and a non-smallest piece. When moving the smallest piece, always move it to the next position in the same direction (to the right if the starting number of pieces is even, to the left if the starting number of pieces is odd). If there is no tower position in the chosen direction, move the piece to the opposite end, but then continue to move in the correct direction. For example, if you started with three pieces, you would move the smallest piece to the opposite end, then continue in the left direction after that. When the turn is to move the non-smallest piece, there is only one legal move. Doing this will complete the puzzle using the fewest number of moves to do so.<ref>{{cite journal |last=Troshkin |first=M. |title=Doomsday Comes: A Nonrecursive Analysis of the Recursive Towers-of-Hanoi Problem |journal=Focus |volume=95 |issue=2 |pages=10–14 |year= |language=Russian }}</ref>
| | <li>[http://www.samprasfanz.com/cgi-bin/guestbook3/guestbook.cgi http://www.samprasfanz.com/cgi-bin/guestbook3/guestbook.cgi]</li> |
| | | |
| ==== Simpler statement of iterative solution ====
| | <li>[http://cgi.www5a.biglobe.ne.jp/~kohei_y/cgi-bin/yybbs.cgi http://cgi.www5a.biglobe.ne.jp/~kohei_y/cgi-bin/yybbs.cgi]</li> |
| Alternating between the smallest and the next-smallest disks, follow the steps for the appropriate case:
| | |
| | | </ul> |
| 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 complete
| |
| | |
| In each case, a total of 2ⁿ-1 moves are made.
| |
| | |
| ==== Equivalent iterative solution ====
| |
| | |
| Another way to generate the unique optimal iterative solution:
| |
| | |
| Number the disks 1 through n (largest to smallest).
| |
| * If n is odd, the first move is from the Start to the Finish peg.
| |
| * If n is even, the first move is from the Start to the Using peg.
| |
| | |
| Now, add these constraints:
| |
| * No odd disk may be placed directly on an odd disk.
| |
| * No even disk may be placed directly on an even disk.
| |
| * Never undo your previous move (that is, do not move a disk back to its immediate last peg).
| |
| Considering those constraints after the first move, there is only one legal move at every subsequent turn.
| |
| | |
| The sequence of these unique moves is an optimal solution to the problem equivalent to the iterative solution described above.<ref>{{cite journal |first1=Herbert |last1=Mayer |first2=Don |last2=Perkins |title=Towers of Hanoi Revisited |pages=80–84 |journal=SIGPLAN Notices |year=1984 |doi=10.1145/948566.948573}}</ref>
| |
| | |
| === Recursive solution ===
| |
| | |
| A 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
| |
| * let ''n'' be the total number of discs
| |
| * number the discs from 1 (smallest, topmost) to ''n'' (largest, bottommost)
| |
| | |
| To move ''n'' discs from peg A to peg C:
| |
| # move ''n''−1 discs from A to B. This leaves disc ''n'' alone on peg A
| |
| # move disc ''n'' from A to C
| |
| # move ''n''−1 discs from B to C so they sit on disc ''n''
| |
| | |
| The 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. This step, moving a single disc from peg A to peg B, is trivial. This approach can be given a rigorous mathematical formalism with the theory of [[dynamic programming]],<ref>{{cite journal |first=Moshe |last=Sniedovich |title= OR/MS Games: 2. The Towers of Hanoi Problem, |journal=INFORMS Transactions on Education |volume=3 |issue=1 |year=2002 |pages=34–51 |url=http://archive.ite.journal.informs.org/Vol3No1/Sniedovich/}}</ref><ref>{{cite book | last = Sniedovich | first = Moshe | title = Dynamic Programming: Foundations and Principles | publisher = Taylor & Francis | year = 2010 | isbn = 978-0-8247-4099-3 |url= http://www.taylorandfrancis.com/books/details/9780824740993/}}</ref> and is often used as an example of recursion when teaching programming.
| |
| | |
| ==== Logical analysis of the recursive solution ====
| |
| | |
| <!-- TODO: This section needs copy-editing for tone, professionalism and clarity: edited/bolded .-->
| |
| As in many mathematical puzzles, finding a solution is made easier by solving a slightly more general problem: how to move a tower of h (h=height) disks from a starting peg '''A''' (f=from) onto a destination peg '''C''' (t=to), '''B''' being the remaining third peg and assuming '''t'''≠'''f'''. First, observe that the problem is symmetric for permutations of the names of the pegs ([[Symmetric group|symmetric group S<sub>''3''</sub>]]). If a solution is known moving from peg '''A''' to peg '''C''', then, by renaming the pegs, the same solution can be used for every other choice of starting and destination peg. If there is only one disk (or even none at all), the problem is trivial. If h=1, then simply move the disk from peg '''A''' to peg '''C'''. If h>1, then somewhere along the sequence of moves, the largest disk must be moved from peg '''A''' to another peg, preferably to peg '''C'''. The only situation that allows this move is when all smaller h-1 disks are on peg '''B'''. Hence, first all h-1 smaller disks must go from '''A''' to '''B'''. Subsequently move the largest disk and finally move the h-1 smaller disks from peg '''B''' to peg '''C'''. The presence of the largest disk does not impede any move of the h-1 smaller disks and can temporarily be ignored. Now the problem is reduced to moving h-1 disks from one peg to another one, first from '''A''' to '''B''' and subsequently from '''B''' to '''C''', but the same method can be used both times by renaming the pegs. The same strategy can be used to reduce the h-1 problem to h-2, h-3, and so on until only one disk is left. This is called recursion. This algorithm can be schematized as follows. Identify the disks in order of increasing size by the natural numbers from 0 up to but not including h. Hence disk 0 is the smallest one and disk h-1 the largest one.
| |
| | |
| The following is a procedure for moving a tower of h disks from a peg '''A''' onto a peg '''C''', with '''B''' being the remaining third peg:
| |
| *Step 1: If h>1 then first use this procedure to move the h-1 smaller disks from peg '''A''' to peg '''B'''.
| |
| *Step 2: Now the largest disk, i.e. disk h can be moved from peg '''A''' to peg '''C'''.
| |
| *Step 3: If h>1 then again use this procedure to move the h-1 smaller disks from peg '''B''' to peg '''C'''.
| |
| | |
| By means of [[mathematical induction]], it is easily proven that the above procedure requires the minimal number of moves possible, and that the produced solution is the only one with this minimal number of moves. Using [[recurrence relation]]s, the exact number of moves that this solution requires can be calculated by: <math>2^h - 1</math>. This result is obtained by noting that steps 1 and 3 take <math>T_{h-1}</math> moves, and step 2 takes one move, giving <math>T_h = 2T_{h-1} + 1</math>.
| |
| | |
| === Non-recursive solution ===
| |
| The list of moves for a tower being carried from one peg onto another one, as produced by the recursive algorithm has many regularities. When counting the moves starting from 1, the ordinal of the disk to be moved during move ''m'' is the number of times ''m'' can be divided by 2. Hence every odd move involves the smallest disk. It can also be observed that the smallest disk traverses the pegs f, t, r, f, t, r, etc. for odd height of the tower and traverses the pegs f, r, t, f, r, t, etc. for even height of the tower. This provides the following algorithm, which is easier, carried out by hand, than the recursive algorithm.
| |
| | |
| In alternate moves:
| |
| * move the smallest disk to the peg it has not recently come from.
| |
| * move another disk legally (there will be one possibility only)
| |
| For the very first move, the smallest disk goes to peg t if h is odd and to peg r if h is even.
| |
| | |
| Also observe that:
| |
| * Disks whose ordinals have even parity move in the same sense as the smallest disk.
| |
| * Disks whose ordinals have odd parity move in opposite sense.
| |
| * If h is even, the remaining third peg during successive moves is t, r, f, t, r, f, etc.
| |
| * If h is odd, the remaining third peg during successive moves is r, t, f, r, t, f, etc.
| |
| | |
| With this knowledge, a set of disks in the middle of an optimal solution can be recovered with no more state information than the positions of each disk:
| |
| * Call the moves detailed above a disk's 'natural' move.
| |
| * Examine the smallest top disk that is not disk 0, and note what its only (legal) move would be: (if there is no such disc, then we are either at the first or last move).
| |
| * If that move is the disk's 'natural' move, then the disc has not been moved since the last disc 0 move, and that move should be taken.
| |
| * If that move is not the disk's 'natural' move, then move disk 0.
| |
| | |
| === Binary solution ===
| |
| Disk positions may be determined more directly from the [[binary numeral system|binary]] (base 2) representation of the move number (the initial state being move #0, with all digits 0, and the final state being #2<sup>''n''</sup>−1, with all digits 1), using the following rules:
| |
| * There is one binary digit ([[bit]]) for each disk
| |
| * The most significant (leftmost) bit represents the largest disk. A value of 0 indicates that the largest disk is on the initial peg, while a 1 indicates that it's on the final peg.
| |
| * The bitstring is read from left to right, and each bit can be used to determine the location of the corresponding disk.
| |
| * A bit with the same value as the previous one means that the corresponding disk is stacked on top the previous disk on the same peg.
| |
| ** (That is to say: a straight sequence of 1's or 0's means that the corresponding disks are all on the same peg).
| |
| * A bit with a different value to the previous one means that the corresponding disk is one position to the left or right of the previous one. Whether it is left or right is determined by this rule:
| |
| ** Assume that the initial peg is on the left and the final peg is on the right.
| |
| ** Also assume "wrapping" - so the right peg counts as one peg "left" of the left peg, and vice versa.
| |
| ** Let n be the number of greater disks that are located on the same peg as their first greater disk and add 1 if the largest disk is on the left peg. If n is even, the disk is located one peg to the left, if n is odd, the disk located one peg to the right.
| |
| | |
| For example, in an 8-disk Hanoi:
| |
| * Move 0 = 00000000
| |
| ** The largest disk is 0, so it is on the left (initial) peg.
| |
| ** All other disks are 0 as well, so they are stacked on top of it. Hence all disks are on the initial peg.
| |
| * Move 2<sup>8</sup>-1 = 11111111
| |
| ** The largest disk is 1, so it is on the right (final) peg.
| |
| ** All other disks are 1 as well, so they are stacked on top of it. Hence all disks are on the final peg and the puzzle is complete.
| |
| * Move 216<sub>10</sub> = 11011000
| |
| ** The largest disk is 1, so it is on the right (final) peg.
| |
| ** Disk two is also 1, so it is stacked on top of it, on the right peg.
| |
| ** Disk three is 0, so it is on another peg. Since n is odd(n=3), it is one peg to the right, i.e. on the left peg.
| |
| ** Disk four is 1, so it is on another peg. Since n is even(n=2), it is one peg to the left, i.e. on the right peg.
| |
| ** Disk five is also 1, so it is stacked on top of it, on the right peg.
| |
| ** Disk six is 0, so it is on another peg. Since n is odd(n=5), the disk is one peg to the right, i.e. on the left peg.
| |
| ** Disks seven and eight are also 0, so they are stacked on top of it, on the left peg.
| |
| | |
| The source and destination pegs for the ''m''th move can also be found elegantly from the binary representation of ''m'' using [[bitwise operation]]s. To use the syntax of the [[C (programming language)|C programming language]], move ''m'' is from peg <code>(m&m-1)%3</code> to peg <code>((m|m-1)+1)%3</code>, where the disks begin on peg 0 and finish on peg 1 or 2 according as whether the number of disks is even or odd. Another formulation is from peg <code>(m-(m&-m))%3</code> to peg <code>(m+(m&-m))%3</code>.
| |
| | |
| Furthermore the disk to be moved is determined by the number of times the move count (m) can be divided by 2 (i.e. the number of zero bits at the right), counting the first move as 1 and identifying the disks by the numbers 0, 1, 2 etc. in order of increasing size. This permits a very fast non-recursive computer implementation to find the positions of the disks after m moves without reference to any previous move or distribution of disks.
| |
| | |
| The [[count trailing zeros]] (ctz) operation, which counts the number of consecutive zeros at the end of a binary number, gives a simple solution to the problem: the disks are numbered from zero, and at move ''m'', disk number ctz(''m'') is moved the minimum possible distance to the right (circling back around to the left as needed).<ref>{{cite book |last=Warren |first=Henry S. |title=Hacker's delight |year=2003 |publisher=Addison-Wesley |location=Boston MA |isbn=0-201-91465-4 |edition=1st |chapter=Section 5-4: Counting Trailing 0's.}}</ref>
| |
| | |
| === Gray code solution ===
| |
| The binary numeral system of [[Gray code]]s gives an alternative way of solving the puzzle. In the Gray system, numbers are expressed in a binary combination of 0s and 1s, but rather than being a standard [[numeral system|positional numeral system]], Gray code operates on the premise that each value differs from its predecessor by only one (and exactly one) bit changed. The number of bits present in Gray code is important, and leading zeros are not optional, unlike in positional systems.
| |
| | |
| If one counts in Gray code of a bit size equal to the number of disks in a particular Tower of Hanoi, begins at zero, and counts up, then the bit changed each move corresponds to the disk to move, where the least-significant-bit is the smallest disk and the most-significant-bit is the largest.
| |
| | |
| :Counting moves from 1 and identifying the disks by numbers starting from 0 in order of increasing size, the ordinal of the disk to be moved during move m is the number of times m can be divided by 2.
| |
| | |
| This technique identifies which disk to move, but not where to move it to. For the smallest disk there are always two possibilities. For the other disks there is always one possibility, except when all disks are on the same peg, but in that case either it is the smallest disk that must be moved or the objective has already been achieved. Luckily, there is a rule which does say where to move the smallest disk to. Let f be the starting peg, t the destination peg and r the remaining third peg. If the number of disks is odd, the smallest disk cycles along the pegs in the order f→t→r→f→t→r, etc. If the number of disks is even, this must be reversed: f→r→t→f→r→t etc.<ref>{{cite book |first=Charles D. |last=Miller |chapter=Ch. 4: Binary Numbers and the Standard Gray Code |chapterurl=https://web.archive.org/web/20040821062630/http://occawlonline.pearsoned.com/bookbind/pubbooks/miller2_awl/chapter4/essay1/deluxe-content.html#tower |title=Mathematical Ideas |edition=9 |publisher=Addison Wesley Longman |year=2000 |isbn=0321076079 }}</ref>
| |
| | |
| == Graphical representation ==
| |
| The game can be represented by an undirected [[graph (mathematics)|graph]], the nodes representing distributions of disks and the edges representing moves. For one disk, the graph is a triangle:
| |
| | |
| [[Image:Tower of Hanoi 1-disk graph.svg|center]]
| |
| | |
| The graph for two disks is three triangles arranged in a larger triangle:
| |
| | |
| [[Image:Tower of Hanoi-2.svg|center]]
| |
| | |
| The nodes at the vertices of the outermost triangle represent distributions with all disks on the same peg.
| |
| | |
| For h+1 disks, take the graph of h disks and replace each small triangle with the graph for two disks.
| |
| | |
| For three disks the graph is:
| |
| | |
| [[Image:Tower of Hanoi-3.svg|440px|center]]
| |
| * call the pegs a, b and c
| |
| * list disk positions from left to right in order of increasing size
| |
| | |
| The sides of the outermost triangle represent the shortest ways of moving a tower from one peg to another one. The edge in the middle of the sides of the largest triangle represents a move of the largest disk. The edge in the middle of the sides of each next smaller triangle represents a move of each next smaller disk. The sides of the smallest triangles represent moves of the smallest disk.
| |
| | |
| [[Image:Hanoi-Graph-7.svg|thumb|right|The game graph of level 7 shows the relatedness to the [[Sierpiński triangle]].]]
| |
| In general, for a puzzle with ''n'' disks, there are 3<sup>''n''</sup> nodes in the graph; every node has three edges to other nodes, except the three corner nodes, which have two: it is always possible to move the smallest disk to one of the two other pegs; and it is possible to move one disk between those two pegs ''except'' in the situation where all disks are stacked on one peg. The corner nodes represent the three cases where all the disks are stacked on one peg. The diagram for ''n'' + 1 disks is obtained by taking three copies of the ''n''-disk diagram—each one representing all the states and moves of the smaller disks for one particular position of the new largest disk—and joining them at the corners with three new edges, representing the only three opportunities to move the largest disk. The resulting figure thus has 3<sup>''n''+1</sup> nodes and still has three corners remaining with only two edges.
| |
| | |
| As more disks are added, the graph representation of the game will resemble a [[fractal]] figure, the [[Sierpiński triangle]]. It is clear that the great majority of positions in the puzzle will never be reached when using the shortest possible solution; indeed, if the priests of the legend are using the longest possible solution (without re-visiting any position), it will take them 3<sup>64</sup> − 1 moves, or more than 10<sup>23</sup> years.
| |
| | |
| The longest non-repetitive way for three disks can be visualized by erasing the unused edges:
| |
| | |
| [[Image:Tower of Hanoi-3 Longest Path.svg|440px|center]]
| |
| | |
| Incidentally, this longest non-repetitive path can be obtained by forbidding all moves from ''a'' to ''b''.
| |
| | |
| The circular [[Hamiltonian path]] for three disks is:
| |
| | |
| [[Image:Tower of Hanoi-4 Longest Cycle.svg|440px|center]]
| |
| | |
| The graphs clearly show that:
| |
| * From every arbitrary distribution of disks, there is exactly one shortest way to move all disks onto one of the three pegs.
| |
| * Between every pair of arbitrary distributions of disks there are one or two different shortest paths.
| |
| * From every arbitrary distribution of disks, there are one or two different longest non selfcrossing paths to move all disks to one of the three pegs.
| |
| * Between every pair of arbitrary distributions of disks there are one or two different longest non self-crossing paths.
| |
| * Let ''N''<sub>''h''</sub> be the number of non selfcrossing paths for moving a tower of ''h'' disks from one peg to another one. Then:
| |
| ** ''N''<sub>1</sub> = 2
| |
| ** ''N''<sub>''h''+1</sub> = (''N''<sub>''h''</sub>)<sup>2</sup> + (''N''<sub>''h''</sub>)<sup>3</sup>.
| |
| ** For example: ''N''<sub>8</sub> ≈ 1.5456×10<sup>795</sup>
| |
| | |
| == Applications ==
| |
| The Tower of Hanoi is frequently used in psychological research on [[problem solving]]. There also exists a variant of this task called [[Tower of London Test|Tower of London]] for neuropsychological diagnosis and treatment of executive functions.
| |
| | |
| The Tower of Hanoi is also used as a [[Backup rotation scheme]] when performing computer data [[Backups]] where multiple tapes/media are involved.
| |
| | |
| As mentioned above, the Tower of Hanoi is popular for teaching recursive algorithms to beginning programming students. A pictorial version of this puzzle is programmed into the [[emacs]] editor, accessed by typing M-x hanoi. There is also a sample algorithm written in [[Prolog]].
| |
| | |
| The Tower of Hanoi is also used as a test by neuropsychologists trying to evaluate [[frontal lobe]] deficits.
| |
| | |
| In 2010, researchers published the results of an experiment that found that the ant species [[linepithema humile]] were successfully able to solve the Tower of Hanoi problem through non-linear dynamics and pheromone signals.<ref>{{cite journal |last1=Reid |first1=C.R. |last2=Sumpter |first2=D.J. |last3=Beekman |first3=M. |title=Optimisation in a natural system: Argentine ants solve the Towers of Hanoi |journal=J. Exp. Biol. |volume=214 |issue=Pt 1 |pages=50–8 |date=January 2011 |pmid=21147968 |doi=10.1242/jeb.048173 |url=http://jeb.biologists.org/cgi/pmidlookup?view=long&pmid=21147968}}</ref>
| |
| | |
| == General shortest paths and the number 466/885 ==
| |
| A curious generalization of the original goal of the puzzle is to start from a given configuration of the disks where all disks are not necessarily on the same peg, and to arrive in a minimal number of moves at another given configuration. In general it can be quite difficult to compute a shortest sequence of moves to solve this problem. A solution was proposed by Andreas Hinz, and is based on the observation that in a shortest sequence of moves, the largest disk that needs to be moved (obviously one may ignore all of the largest disks that will occupy the same peg in both the initial and final configurations) will move either exactly once or exactly twice.
| |
| <br/> | |
| <br/>The mathematics related to this generalized problem becomes even more interesting when one considers the '''average''' number of moves in a shortest sequence of moves between two initial and final disk configurations that are chosen at random. Hinz and Chan Hat-Tung independently discovered | |
| <ref>{{cite journal |first=A. |last=Hinz |title=The Tower of Hanoi |journal=L'Enseignement Mathematique |volume=35 |year=1989 |pages=289–321 |doi=10.5169/seals-57378}}</ref><ref>{{cite journal |first=T. |last=Chan |title=A statistical analysis of the towers of Hanoi problem |journal=Internat. J. Comput. Math. |volume=28 |year=1988 |pages=57–65 |doi=10.1080/00207168908803728}}</ref> (see also,
| |
| <ref>{{cite book |last=Stewart |first=Ian |title=Another Fine Math You've Got Me Into... |publisher=Courier Dover |year=2004 |isbn=0-7167-2342-5 |chapter=}}</ref> Chapter 1, p. 14)
| |
| that the average number of moves in an n-disk Tower is given by the following exact formula:
| |
| | |
| :<math> \frac{466}{885}\cdot 2^n - \frac{1}{3} - \frac{3}{5}\cdot \left(\frac{1}{3}\right)^n +
| |
| \left(\frac{12}{29} + \frac{18}{1003}\sqrt{17}\right)\left(\frac{5+\sqrt{17}}{18}\right)^n +
| |
| \left(\frac{12}{29} - \frac{18}{1003}\sqrt{17}\right)\left(\frac{5-\sqrt{17}}{18}\right)^n.</math>
| |
| | |
| Note that for large enough ''n'', only the first and second terms do not converge to zero, so we get an [[asymptotic analysis|asymptotic expression]]: <math>466/885\cdot 2^n - 1/3 + o(1)</math>, as <math>n \to \infty</math>. Thus intuitively, we could interpret the fraction of <math>466/885\approx 52.6\%</math> as representing the ratio of the labor one has to perform when going from a randomly chosen configuration to another randomly chosen configuration, relative to the difficulty of having to cross the "most difficult" path of length <math>2^n-1</math> which involves moving all the disks from one peg to another. An alternative explanation for the appearance of the constant 466/885, as well as a new and somewhat improved algorithm for computing the shortest path, was given by Romik.<ref>{{cite journal |first=D. |last=Romik |title=Shortest paths in the Tower of Hanoi graph and finite automata |journal=[[SIAM Journal on Discrete Mathematics]] |volume=20 |issue=3 |year=2006 |pages=610–622 |doi=10.1137/050628660}}</ref>
| |
| | |
| == Variations ==
| |
| | |
| === Cyclic Hanoi ===
| |
| Cyclic Hanoi is a variation of the Hanoi in which each disk must be moved in the same cyclic direction, in most cases, clockwise.<ref>{{cite journal |first=T. D. |last=Gedeon |title=The Cyclic Towers of Hanoi: An Iterative Solution Produced by Transformation |journal=The Computer Journal |volume=39 |issue=4 |year=1996 |doi=10.1093/comjnl/39.4.353 |url=http://comjnl.oxfordjournals.org/content/39/4/353.short}}</ref> For example, given a standard three peg set-up, a given disk can be moved from peg A to peg B, then from B to C, C to A, etc. This can be solved using two mutually recursive procedures:
| |
| | |
| To move ''n'' discs '''clockwise''' from peg A to peg C:
| |
| # move ''n'' − 1 discs '''clockwise''' from A to C
| |
| # move disc #''n'' from A to B
| |
| # move ''n'' − 1 discs '''counterclockwise''' from C to A
| |
| # move disc #''n'' from B to C
| |
| # move ''n'' − 1 discs '''clockwise''' from A to C
| |
| | |
| To move ''n'' discs '''counterclockwise''' from peg A to peg C:
| |
| | |
| # move ''n'' − 1 discs '''clockwise''' from A to B
| |
| # move disc #''n'' from A to C
| |
| # move ''n'' − 1 discs '''clockwise''' from B to C
| |
| | |
| === With four pegs and beyond ===
| |
| Although the three-peg version has a simple recursive solution as outlined above, the ''optimal'' solution for the Tower of Hanoi problem with four pegs (called '''Reve's puzzle'''), let alone more pegs, is still an [[open problem]]. This is a good example of how a simple, solvable problem can be made dramatically more difficult by slightly loosening one of the problem constraints.
| |
| | |
| The fact that the problem with four or more pegs is an open problem does not imply that no algorithm exists for finding (all of) the optimal solutions. Simply represent the game by an undirected graph, the nodes being distributions of disks and the edges being moves and use [[breadth first search]] to find one (or all) shortest path(s) moving a tower from one peg onto another one. However, even smartly implemented on the fastest computer now available, this algorithm provides no way of effectively computing solutions for large numbers of disks; the program would require more time and memory than available. Hence, even having an algorithm, it remains unknown how many moves an optimal solution requires and how many optimal solutions exist for 1000 disks and 10 pegs.
| |
| | |
| Though it is not known exactly how many moves must be made, there are some asymptotic results. There is also a "presumed-optimal solution" given by the '''Frame-Stewart algorithm''', discovered independently by Frame and Stewart in 1941.<ref>{{cite journal |first1=B.M. |last1=Stewart |first2=J.S. |last2=Frame |title=Solution to advanced problem 3819 |journal=American Mathematical Monthly |volume=48 |issue=3 |pages=216–9 |date=March 1941 |jstor=2304268}}</ref> The related open Frame-Stewart conjecture claims that the Frame-Stewart algorithm always gives an optimal solution. The optimality of the Frame-Stewart algorithm has been computationally verified for 4 pegs with up to 30 disks.<ref>{{cite journal |last1=Korf |first1=Richard E. |first2=Ariel |last2=Felner |title=Recent Progress in Heuristic Search: a Case Study of the Four-Peg Towers of Hanoi Problem |journal=[[IJCAI]] |year=2007 |pages=2324–9 |url=http://www.ijcai.org/papers07/Papers/IJCAI07-374.pdf |format=PDF}}</ref>
| |
| | |
| For other variants of the four-peg Tower of Hanoi problem, see Paul Stockmeyer's survey paper.<ref>{{cite journal |first=Paul |last=Stockmeyer |title=Variations on the Four-Post Tower of Hanoi Puzzle |journal=Congressus Numerantium |volume=102 |year=1994 |pages=3–12 |url=http://www.cs.wm.edu/~pkstoc/boca.ps |format=Postscript}}</ref>
| |
| | |
| ==== Frame–Stewart algorithm ====
| |
| The Frame–Stewart algorithm, giving a ''presumably optimal solution'' for four (or even more) pegs, is described below:
| |
| * Let <math>n</math> be the number of disks.
| |
| * Let <math>r</math> be the number of pegs.
| |
| * Define <math>T(n,r)</math> to be the minimum number of moves required to transfer n disks using r pegs
| |
| The algorithm can be described recursively:
| |
| # For some <math>k</math>, <math>1 \leq k < n</math>, transfer the top <math>k</math> disks to a single peg other than the start or destination pegs, taking <math>T(k,r)</math> moves.
| |
| # Without disturbing the peg that now contains the top <math>k</math> disks, transfer the remaining <math>n-k</math> disks to the destination peg, using only the remaining <math>r-1</math> pegs, taking <math>T(n-k,r-1)</math> moves.
| |
| # Finally, transfer the top <math>k</math> disks to the destination peg, taking <math>T(k,r)</math> moves.
| |
| The entire process takes <math>2T(k,r)+T(n-k,r-1)</math> moves. Therefore, the count <math>k</math> should be picked for which this quantity is minimum.
| |
| | |
| This algorithm (with the above choice for <math>k</math>) is presumed to be optimal, and no counterexamples are known.
| |
| | |
| === Multistack Tower of Hanoi ===
| |
| {{unreferenced section|date=November 2013}}
| |
| U.S. patent number 7,566,057 issued to Victor Mascolo discloses multistack Tower of Hanoi puzzles with two or more stacks and twice as many pegs as stacks. After beginning on a particular peg, each stack displaces and is displaced by a different colored stack on another peg when the puzzle is solved. Disks of one color also have another peg that excludes all other colors, so that there are three pegs available for each color disk, two that are shared with other colors, and one that is not shared. On the shared pegs, a disk may not be placed on a different colored disk of the same size, a possibility that does not arise in the standard puzzle.
| |
| | |
| The simplest multistack game, Tower of Hanoi (2 × 4), has two stacks and four pegs, and it requires 3[''T''(''n'')] moves to solve where ''T''(''n'') is the number of moves needed to solve a single stack classic of ''n'' disks. The game proceeds in seesaw fashion with longer and longer series of moves that alternate between colors. It concludes in reverse seesaw fashion with shorter and shorter such series of moves. Starting with the second series of three moves, these alternate series of moves double in length for the first half of the game, and the lengths are halved as the game concludes. The solution involves nesting an algorithm suitable for Tower of Hanoi into an algorithm that indicates when to switch between colors. When there are k stacks of n disks apiece in a game, and ''k'' > 2, it requires ''k''[''T''(''n'')] + ''T''(''n'' − 1) + 1 moves to relocate them.
| |
| | |
| The addition of a centrally located universal peg open to disks from all stacks converts these multistack Tower of Hanoi puzzles to multistack Reve's puzzles as described in the preceding section. In these games each stack may move among four pegs, the same combination of three in the 2 × 4 game plus the central universal peg. The simplest game of this kind (2 × 5) has two stacks and five pegs. A solution conjectured to be optimal interlocks the optimal solution of the 2 × 4 puzzle with the presumed optimal solution to Reve's puzzle. It takes ''R''(''n'') + 2''R''(''n'' − 1) + 2 moves, where ''R''(''n'') is the number of moves in the presumed optimal Reve's solution for a stack of ''n'' disks.
| |
| | |
| == In popular culture ==
| |
| In the science fiction story "Now Inhale", by [[Eric Frank Russell]],<ref>{{cite journal |journal=Astounding Science Fiction |date=April 1959 |first=Eric Frank |last=Russell |title=Now Inhale}}</ref> the human is a prisoner on a planet where the local custom is to make the prisoner play a game until it is won or lost, and then his execution will be immediate. The protagonist knows that a rescue ship might take a year or more to arrive, so he chooses to play Towers of Hanoi with 64 disks. (This story makes reference to the legend about the Buddhist monks playing the game until the end of the world.)
| |
| | |
| In the 1966 ''[[Doctor Who]]'' story ''[[The Celestial Toymaker]]'', the [[eponym]]ous villain forces [[The Doctor (Doctor Who)|the Doctor]] to play a ten-piece 1,023-move Tower of Hanoi game entitled [[List of Doctor Who items#T|The Trilogic Game]] with the pieces forming a pyramid shape when stacked.
| |
| | |
| In 2007, the concept of the Towers Of Hanoi problem was used in ''[[Professor Layton and the Diabolical Box]]'' in puzzles 6, 83, and 84, but the discs had been changed to pancakes. The puzzle was based around a dilemma where the chef of a restaurant had to move a pile of pancakes from one plate to the other with the basic principles of the original puzzle (i.e. three plates that the pancakes could be moved onto, not being able to put a larger pancake onto a smaller one, etc.)
| |
| | |
| In the film ''[[Rise of the Planet of the Apes]]'' (2011), this puzzle, called in the film the "Lucas Tower", is used as a test to study the intelligence of apes.
| |
| | |
| The puzzle is featured regularly in [[adventure game|adventure]] and [[computer puzzle game|puzzle]] games. Since it is easy to implement, and easily recognised, it is well-suited to use as a puzzle in a larger graphical game (e.g. ''[[Star Wars: Knights of the Old Republic]]'' and ''[[Mass Effect (video game)|Mass Effect]]''<ref>{{cite web |url=http://www.giantbomb.com/tower-of-hanoi/92-5744/ |title=Tower of Hanoi (video game concept) |publisher=giantbomb.com |accessdate=2010-12-05}}</ref>). Some implementations use straight disks, but others disguise the puzzle in some other form. There is an arcade version by [[Sega]]/[[Andamiro]].<ref>http://www.segaarcade.com/towerofhanoi</ref>
| |
| | |
| The problem is featured as part of a reward challenge in a [[Survivor: South Pacific#Episode 1: "I Need Redemption"|2011 episode of the American version of the ''Survivor'' TV series]]. Both players ([[Ozzy Lusth]] and [[Benjamin Wade (Survivor contestant)|Benjamin "Coach" Wade]]) struggle to understand how to solve the puzzle and are aided by their fellow tribe members.
| |
| | |
| == See also ==
| |
| *[[Backup rotation scheme]], a TOH application
| |
| *[[Baguenaudier]]
| |
| *[[Recursion (computer science)]]
| |
| | |
| == Notes ==
| |
| {{reflist|30em}}
| |
| | |
| == External links ==
| |
| {{Commons category|Tower of Hanoi}}
| |
| *{{mathworld|title=Tower of Hanoi|urlname=TowerofHanoi}}
| |
| *{{dmoz|Science/Math/Recreations/Famous_Problems/Tower_of_Hanoi|Tower of Hanoi}}
| |
| *[http://www.codeminima.com/Lisp:_Tower_of_Hanoi Tower of Hanoi: Code in Lisp]
| |
| *[https://play.google.com/store/apps/details?id=com.ADP.pyramidmovers Tower of Hanoi Variant Game: Pyramid Mover]
| |
| | |
| {{DEFAULTSORT:Tower Of Hanoi}}
| |
| [[Category:Mechanical puzzles]]
| |
| | |
| {{Link GA|de}}
| |
| {{Link FA|he}}
| |