Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi all.

Many computer science professors eventually discuss the concept of recursion. To help illustrate the power and elegance (yes, there are drawbacks as well) it provides, a classic problem known as the 'Towers of Hanoi' is often used. For those unfamiliar with this classic, please allow me explain the history and rules...

The History:

The puzzle is called "Towers of Hanoi" because an early popular presentation wove a fanciful legend around it. According to this myth (uttered long before the Vietnam War), there is a Buddhist monastery at Hanoi which contains a large room with three time-worn posts in it surrounded by 21 golden discs. Monks, acting out the command of an ancient prophecy, have been moving these disks, in accordance with the rules of the puzzle, once every day since the monastery was founded over a thousand years ago. They are said to believe that when the last move of the puzzle is completed, the world will end in a clap of thunder. Fortunately, they are nowhere even close to being done.

The Rules:

  • There are n disks (1, 2, 3,..., n) and three towers (pegs). The towers are labeled 'A', 'B', and 'C'.
  • All the disks are initially placed on the first peg (the 'A' peg).
  • No disk may be placed on top of a smaller disk.
  • You may only move one disk at a time and this disk must be the top disk on a peg.

The Code:

use warnings; use strict; # Towers of Hanoi # Perl version (5.8.0) # Ported from Java my $numdisks = 0; print "Number of disks? "; chomp( $numdisks = <STDIN> ); print "The moves are:\n\n"; movedisks( $numdisks, 'A', 'B', 'C' ); sub movedisks { my( $num, $from, $to, $aux ) = @_; if( $num == 1 ) { print "Move disk $num from $from to $to\n"; } else { movedisks( $num-1, $from, $aux, $to ); print "Move disk $num from $from to $to\n"; movedisks( $num-1, $aux, $to, $from ); } }

Any suggestions on how to 'fine tune' this program are more than welcome.

Hope this proves interesting.


20041023 Edit by Steve_p: Changed title from 'Visiting the Towers of Hanoi.'

In reply to Recursion: The Towers of Hanoi problem by DigitalKitty

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2020-09-25 08:03 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (136 votes). Check out past polls.