This is the 11th day of my participation in Gwen Challenge

Hannota is an ancient Indian legend. There are three columns, one of which is placed on a number of disks, these disks from top to bottom, increasing in diameter, using an auxiliary cylinder, the disc on the original column on another column, still from top to bottom increasing in diameter.

It is

Hannott Tower: The problem with Hannott Tower (also known as Hanoi Tower) is a puzzle toy from an old Indian legend. Mahama made three diamond pillars when he created the world, and on one pillar were stacked 64 gold disks in ascending order of size. Mahama ordered the Brahmin to rearrange the disks on another pillar, starting from below, in order of size. It is also stipulated that the disk cannot be enlarged on the small disk and that only one disk can be moved at a time between the three columns.

recursive

Recursive functions can efficiently manipulate tree structures, such as the browser-side document model DOM, processing a small portion of a given tree each time a recursive call is made. The Hannotta problem can be solved very well using recursion

The solution was to break the problem down into three small problems and move a bunch of disks to another column, using auxiliary columns if necessary. First, the smaller of the pair of disks is moved to the auxiliary column to expose the larger disk, then the underground disk is moved to the target column, and finally the smaller disk is removed from the auxiliary column and moved to the target column. A recursive call to handle the movement of the disk solves the subproblem.

function hanoi (disc, a, b, c) {
    if (disc > 0) {
        hanoi(disc - 1, a, c, b)
        console.log('move disc ' + disc + ' from ' + a + ' to ' + c)
        hanoi(disc - 1, b, a, c)
    }
}

hanoi(4.'source'.'secondary'.'target')

// console
move disc 1 fromSource to assist move disc2 fromSource to target move disc1 fromAssist to target move disc3 fromSource to assist move disc1 fromTarget to source move disc2 fromTarget to assist move Disc1 fromSource to assist move disc4 fromSource to target move disc1 fromAssist to target move disc2 fromAssist to source move disc1 fromTarget to source move disc3 fromAssist to target move disc1 fromSource to assist move disc2 fromSource to target move disc1 fromAuxiliary to targetCopy the code

Tail recursion

Some languages provide tail recursion, a special form of recursion that executes a recursive call statement at the end of a function.

BTW, if a function returns a recursive call of its own, the process of the call is replaced by a loop, which can be significantly faster. See the differences and principles between recursion and tail recursion

The above! Last rule, post my blog, welcome to follow

Please pay attention to the official number: Full stack flying Squadron