(1+R)^Y
. For learning purposes, we wrote our own
exponentiation function.
exp()
exp()
to an O(logn) version. However, the new version
is recursive but not tail recursive. Can we make it tail-recursive
(that is, it does nothing after a recursive call)? Can we make it
iterative? Can we do both without using a separate stack?
exp()
exp()
tail recusive, we'll need to do a few differnt
things.
x^(2k)
is (x^2)^k
.
public static double exp(double x, int n) { return exp(x,n,1.0); } // exp public static double exp(double x, int n, double acc) { if (n == 0) return acc; if (even(n)) return exp(x*x, n/2, acc); else return exp(x, n-1, x*acc); } // exp
exp()
exp()
iterative.
public static double exp(double x, int n) { double acc = 1.0; while (n > 0) { if (even(n)) { x = x*x; n = n / 2; // or n >> 1 } else { acc *= x; n = n - 1; } } // while return acc; } // exp
galton
and robinson
:
original iterative version using doubles
herstein
and stokes
:
original iterative version using ints
jordan
and taylor
:
separate call to iterative exp()
function (using doubles)
klein
and venn
:
recursive O(logn) exp()
function (using doubles)
lie
and wyel
:
tail-recursive O(logn) exp()
(using doubles)
markov
and wronski
:
iterative O(logn) exp()
(using doubles)
neyman
and young
:
iterative O(logn) exp()
(using ints)
peano
and zorn
:
precompute a table of present values for 3% after one to ten years
(inclusive) and use that table.
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.
Source text last modified Mon Nov 10 08:58:14 1997.
This page generated on Mon Nov 10 10:12:59 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu