mkdir ~/cs170/lab11 cd ~cs170002/share/lab11 cp *.java ~/cs170/lab11 cd ~/cs170/lab11 
We will learn to write 2 recursive algorithms in this lab.
But first, some theory on how to think about recursive algorithms.
Example: factorial
public static int factorial( int n ) { int sol1, mySol; if ( n == 0 ) { return 1; // A solution is available for a base case } else { sol1 = factorial ( n  1 ); mySol = n * sol1; return mySol; } } 
The original parameter is n, and factorial(n) invokes itself with a smaller parameter value (n−1)

So:




public static ReturnType solveProblem( int n ) { ReturnType sol1, mySol; if ( n is a base case ) { return solution; // A solution is available for a base case } else { sol1 = solveProblem ( n  1 ); mySol = solve problem of size n using solution sol1; return mySol; } } 
Example: factorial
public static int factorial( int n ) { int sol1, mySol; if ( n == 0 ) { return 1; // A solution is available for a base case } else { sol1 = factorial ( n  1 ); mySol = n * sol1; return mySol; } } 


But, that is not always the case.
The most common smaller problem that a "string typed" recursive method solves within itself uses a string of shorter length n−1.


cd ~/cs170/lab11 /home/cs170002/turninlab Power.java lab11 /home/cs170002/turninlab SumDigits.java lab11a 
For the Lead TA:
