|
-
Jun 28th, 2010, 11:32 PM
#1
Thread Starter
New Member
Easy question from a really new guy
Hi guys, I am really new to programming and I am reading "How to Think Like a Computer Scientist" in order to learn. I really like the book. Anyway, I have been doing all the exercises and I have become stumped. Here is my problem:
Changing
total = total + Math.pow(x,i)/factorial(i);
to
total = total + total*(x/i);
Yields an entirely different result.
Prolbem set B: You can make this method much more efficient if you realize that in each iter-
ation the numerator of the term is the same as its predecessor multiplied by x
and the denominator is the same as its predecessor multiplied by i. Use this
observation to eliminate the use of Math.pow and factorial, and check that
you still get the same result.
Whole program:
public static double myExp(double x){
double i = 1;
double total = 1;
while(i < 555){
total = total + (total*(x/i);
i++;
}
return total;
}
public static double factorial(double x){
double i = x;
double total = 1;
while(i > 1){
total = i*total;
i--;
}
return total;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|