[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to Binary Search. On to A Stamp Problem.
Held Tuesday, February 23
Summary
Contents
Notes
public returnType tailRecursiveAlgorithm(someType input, someType extraInfo) {
someType simpler_input;
if (baseCase(input)) {
return baseComputation(input);
}
else {
simpler_input = simplify(input);
return tailRecursiveAlgorithm(simpler_input,more_stuff));
} // recursive case
} // tailRecursiveAlgorithm
public returnType recursiveAlgorithm(someType input, someType extraInfo) {
someType simplerInput;
someType recursiveResult;
if (baseCase(input)) {
return baseComputation(input);
}
else {
simplerInput = simplify(input);
moreStuff = doSomeNonrecursiveComputations(input, extraInfo);
recursiveResult = recursiveAlgorithm(simplerInput,moreStuff));
return doMoreWith(recursiveResult);
} // recursive case
} // recursiveAlgorithm
return doMoreWith(recursiveAlgorithm(simplify(input), ...));
/**
* Compute n!
* <br>Precondition: n >= 0
* <br>Precondition: n! <= Integer.MAX_VALUE
* <br>Postcondition: returns n!
*/
public int factorial(int n) {
// Base case. 0! = 1.
if (n == 0) return 1;
// Recursive case. n! = n * ((n-1)!)
else return n * factorial(n-1);
} // factorial(int)
factorial we multiply by n.
/**
* Compute n!
* <br>Precondition: n >= 0
* <br>Precondition: n! <= Integer.MAX_VALUE
* <br>Postcondition: returns n!
*/
public int factorial(int n) {
return factorial(n, 1);
} // factorial(int)
/**
* Compute acc*n!
* <br>Precondition: n >= 0
* <br>Precondition: (acc*n!) <= Integer.MAX_VALUE
* <br>Postcondition: returns n!
*/
public int factorial(int n, int acc)
{
// Base case. acc*0! = acc*1 = acc.
if (n == 0) return acc;
// Recursive case. acc*(n!) = (acc*n) * ((n-1)!)
else return factorial(n-1, acc*n);
} // factorial(int,int)
/**
* Compute x^n for integers x and n
* <br>Precondition: n >= 0
* <br>Precondition: x > 0
* <br>Precondition: x^n <= Integer.MAX_VALUE (can't really check this, but ...)
* <br>Postcondition: returns x^n
*/
public int exp(int x, int n) {
int result = 1; // The result, computed as we go
for(int i = 0; i < n; ++i) {
result *= x;
}
return result;
} // exp(int, int)
public int exp(int x, int n) {
// Base case. x^0 = 1.
if (n == 0) return 1;
// Recursive case. x^n = x * x^(n-1).
else return x * exp(x, n-1);
} // exp(int, int)
public int exp(int x, int n) {
// Temporary value, used for integer calculations.
int tmp;
// Base case. x^0 = 1.
if (n == 0) return 1.
// Recursive case. x^(2k) = x^k * x^k.
else if (even(n)) {
tmp = exp(x, n/2);
return tmp*tmp;
} // n is even
// Recursive case. x^(k+1) = x * x^k
else { // n is odd
tmp = exp(x, n-1);
return x*tmp;
} // n is odd
} // exp
History
Back to Binary Search. On to A Stamp Problem.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.18.html
Source text last modified Tue Feb 23 14:34:04 1999.
This page generated on Tue Feb 23 14:59:01 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu