[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Recursion. On to Project Discussion.
Held Friday, October 1, 1999
Overview
Notes
Contents
Summary
/**
* An illustration of different mechanisms for computing x^n.
*
* @author Samuel A. Rebelsky
* @version 1.0 of October 1999
*/
public class Exponentiation {
// +---------+-------------------------------------------------
// | Methods |
// +---------+
/**
* Compute x^n by multiplying x*x*...*x n times. Uses
* iteration as the mechanism for repetition.
*/
public static double expA(double x, int n) {
// Handle negative exponents. x^(-n) = 1/(x^n)
if (n < 0) {
return 1/expA(x,-n);
}
// Prepare to compute the product.
double product = 1;
// Multiply by each x. (Note that 1*x*x*...*x = x*x*...*x.)
for (int i = 0; i < n; ++i) {
product = product * x;
} // for
// That's it, we're done.
return product;
} // expA(double,int)
/**
* Compute x^n by multiplying x*x*...*x n times. Uses
* recursion as the mechanism for repetition.
*/
public static double expB(double x, int n) {
// Handle negative exponents. x^(-n) = 1/(x^n)
if (n < 0) {
return 1/expB(x,-n);
} // if (n < 0)
// Base case: x^0 is 1.
if (n == 0) {
return 1;
} // if (n == 0)
// Recursive case: x*x*...*x = x*(x*...*x)
// That is: x^n = x*(x^(n-1))
else {
return x * expB(x, n-1);
} // Recursive case, if (n > 0)
} // expB(double,int)
/**
* Compute x^n by a recursive divide-and-conquer algorithm.
*/
public static double expC(double x, int n) {
// Handle negative exponents. x^(-n) = 1/(x^n)
if (n < 0) {
return 1/expB(x,-n);
} // if (n < 0)
// Base case: x^0 is 1.
if (n == 0) {
return 1;
} // Base case: n == 0
// Recursive case (when n is odd)
// x*x*...*x = x*(x*...*x)
// That is: x^n = x*(x^(n-1))
else if (n % 2 == 1) {
return x * expC(x,n-1);
} // Recursive case (when n is odd)
// Recursive case (when n is even)
// Let n be 2k
// x^n = x^(2k) = x^k * x^k
else {
int k = n/2;
double tmp = expC(x,k);
return tmp*tmp;
} // Recursive case (when n is even)
} // expC
} // class Exponentiation
expA, is perhaps the easiest to
analyze.
expB, is somewhat more difficult
to analyze because of the recursion.
expB(x,n-1))?
Hmmm ... Wait! fb is supposed to tell us that!
expC, is perhaps even more
difficult to evaluate.
Tuesday, 10 August 1999
Friday, 1 October 1999
Back to Recursion. On to Project Discussion.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [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/99F/Outlines/outline.22.html
Source text last modified Fri Oct 1 16:01:04 1999.
This page generated on Fri Oct 1 16:01:39 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu