SUNY Geneseo Department of Computer Science
{Date}
CSci 141, Fall 2003
Prof. Doug Baldwin
Number of data points for expt: nothing specific, but repeat measurements enough to get a handle on variation
Section 6.1
Recursion = taking complex problem, look at one step at a time
"A journey of 1000 miles begins with a single step"
Equivalent syntaxes?
The Towers of Hanoi
Rules:
Goals:
Algorithm:
A slightly more detailed version of this algorithm is one of the known ways of solving the Towers of Hanoi. Roughly, it alternates moving the smallest disk two poles right (assuming you want to move from the left-most pole to the right-most, and wrapping around at the ends) and moving the next smallest disk to the (only) pole it can move to.
Look at a Recursive Version in Java. The big lesson here is not so much how the recursion works (we'll come back to that after studying recursion longer), but that the recursive algorithm is so compact -- recursion lets you do a lot of work in a small amount of code.
The book's "takeJourney" algorithm, but for robots and with a parameter saying how far to move
takeJourney( int n )
if ( n > 0 )
this.move()
this.takeJourney( n - 1 )
Print the numbers from n (a parameter) down to 0
printDown( int n )
if ( n >= 0 )
System.out.print( n )
this.printDown( n - 1 )
Read Sections 6.2 - 6.4
Mini-assignment: design a recursive algorithm, int powerOf2( int n )
,
that computes and returns 2n