This is where the towers of Hanoi emerge in milliseconds!

Dedicated to Harry Paul on his 91st birthday

In computer science, the Towers of Hanoi (Wiki) are considered a prime example of a problem that can only be solved recursively (or iteratively). The time for calculating a certain position n thus grows exponentially with O(2n). In this article an explicit solution is presented with which one can compute any position n with the same computing time O(1). This explicit solution is used in all animations.

For 10 disks, the rearrangement looks like this (the number of moves is shown at the top, with a 1 second pause for Mersenne numbers): Suppose the monks started moving one disk per second exactly 91 years ago. Then the positions of the disks at times 91a-45s to 91a+45s look like this (the top disk (1) is moved every second move, etc.): Or with disks: The start position 0 (exactly before 91a) is displayed 5 seconds, then it continues every second as above, i.e. with 91a-45s: Congratulations!

© 11.02.2022, Dr. Michael Komma (VGWORT)

Method: Explicit calculation of the position of the disks for a given number of moves. Computing time per position 50ms. For comparison: The time for recursive calculation of the position 91*365*24*3600 = 2869776000 is 6.7h.

Details: The standard procedure for recursively calculating the positions of the disks looks like this:

 > (1)

With four disks and the positions a,b and c one gets e.g.

 > Where 1..4 is the number of the disk (1 for the smallest) that is moved from the first (start) to the last (target) position (the middle position or park position is only listed for completeness). So "1,a,c,b" means: move disk 1 from a to b, and "2,a,b,c": move disk 2 from a to c.

You can see in this example:
1. Odd-numbered disks cycle with (a,b,c) and even-numbered disks cycle with (a,c,b).
2. Disk 1 is moved every other move, disk 2 is moved every fourth move, and disk j is moved every 2j -th move.
3. Note: If the target stack for the complete tower is given as b or c, this can easily be accommodated depending on the number of disks (odd or even).

Since the motion of the disks is cyclic, it cannot be a problem that can only be solved recursively (or iteratively). So there must be an explicit solution. In order to find this explicit solution, one only has to answer the question of how often disk j was moved if a total of n moves were made.

A simple counting procedure is used to find

 > (3)

Visualization for five disks

 > Which reduces the computing time from O(2^n) to O(1).

As is known, the number of the disk that has to be moved can be calculated like this (recursion relation):

 > or like this (explicitly):

 > But then you don't know where the disk is. For the recursion relation one also finds representations of the type (abscissa: number of the move, ordinate: number of the disk that is being moved): or as a tree In particular, the tree representation is read with its branches from top to bottom (computer trees and family trees have their roots at the top :), so not in the order of the moves.
The explicit solution is easier to find by reading from left to right (logarithm of two - shifted up by 1 - as "envelope" in black):
{"ft_lang_label":"__label__en","ft_lang_prob":0.8833549,"math_prob":0.9814538,"size":3048,"snap":"2023-14-2023-23","text_gpt3_token_len":760,"char_repetition_ratio":0.13370565,"word_repetition_ratio":0.03448276,"special_character_ratio":0.24343832,"punctuation_ratio":0.11666667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99116516,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38],"im_url_duplicate_count":[null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-30T11:32:40Z\",\"WARC-Record-ID\":\"<urn:uuid:9a339a03-88d0-4b06-a9d1-3050f8e01062>\",\"Content-Length\":\"19980\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e45931dc-bd8b-4057-802c-da24379a4b22>\",\"WARC-Concurrent-To\":\"<urn:uuid:cdd9f476-7313-42bb-ac94-a11e5b729ab7>\",\"WARC-IP-Address\":\"80.237.130.131\",\"WARC-Target-URI\":\"http://mikomma.eu/hanoi/hanoi_en.htm\",\"WARC-Payload-Digest\":\"sha1:6HXOAJORFSUTTPVOIJMBDINCP4JLG6GC\",\"WARC-Block-Digest\":\"sha1:7PSUURTNAK3LAU3MBEPOQEJCJMBLUCK7\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296949181.44_warc_CC-MAIN-20230330101355-20230330131355-00333.warc.gz\"}"}