/*Write a program that computes the value of by using the e^x = 1 + x/1! + x^2/2! + x^3/3! .....
formulaPrompt the user for the desired accuracy of e (i.e., the
number of terms in the summation)*/
///Luis fernando pinzon
///27/08/2018
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
int main(int argc, char** argv)
{
int counter = 1;
double factorial = 1;
int number;
int storePotencial = 1;
int numberPotencial;
double fraction = 1;
//number of terms
cout << "write the number of terms in the sum:";
cin >> number;
cout << "\n";
//numero of the potential
cout << "write the potencial: ";
cin >> numberPotencial;
cout << "\n";
if(number > 0)//to check if the number of terms are greater than zero
{
if(numberPotencial == 0)//to check the potencial number if zero then the final result is zero
{
fraction = 1;
}
while(counter <= number)
{
///to get the factorial number ----> denominator
factorial *= counter;
///to get the potencial number ----> nominator
storePotencial = storePotencial * numberPotencial;
//sumaPotencia += almacenarPotencia;
//to get the fraction
fraction = fraction + (static_cast<double>(storePotencial)/factorial);
counter++;
}
}
else
{
cout << "the nomber is invalid. ";
}
cout << "the number e raised up to " << numberPotencial << " on the approach " << number << " terms is: ";
cout << setprecision(6) << fixed << fraction << endl;
return 0;
}
like most things dealing with factorials, its going to break pretty quickly for large inputs. You may want to check to see if your factorials are too big to store and reject input for number of terms above some value.
optimization of this is silly; you can just do pow(e, power) or a log to get the answer directly.
the first rule of optimization is to use the best algorithm before tweaking. I don't see anything you could improve anyway; its a pretty tight loop that does exactly what you were directed to do. Factorials could be a lookup table and save you a couple of nanoseconds. For this kind of use case, that is not critical, its the ones where you compute them all every time that are bad, eg for x = 1 to 100 print fact(x) where fact(x) computes from scratch every time is wasteful. Yours is a running product which isnt costing anything.
consistent formatting and commenting, remove dead (commented out ) code, general cleanup. I don't see any major improvements. While counter looks like you used a while loop as a for loop. This is "ok" but a little clunky, use a for loop for a for loop (you can also use a for loop like a while loop, but there again, its ugly to do so). Checking zero ... its not a special case. I believe the math works fine for zero, and the condition burns as much time as it saves. I think you can take that out entirely. Initialize fraction to 1, and add the terms after that; if the power is zero, youll get 1 correctly I think. Number is misspelled in your final output (looks more like mixed up your languages). You may find unsigned ints to be the best choice for some of the variables.
all in all this is very good work.
I am paranoid, but putting one's name on the web is usually a bad idea.
"optimization of this is silly; you can just do pow(e, power) or a log to get the answer directly.
the first rule of optimization is to use the best algorithm before tweaking. I don't see anything you could improve anyway; its a pretty tight loop that does exactly what you were directed to do. Factorials could be a lookup table and save you a couple of nanoseconds. For this kind of use case, that is not critical, its the ones where you compute them all every time that are bad, eg for x = 1 to 100 print fact(x) where fact(x) computes from scratch every time is wasteful. Yours is a running product which isnt costing anything.
consistent formatting and commenting, remove dead (commented out ) code, general cleanup. I don't see any major improvements. While counter looks like you used a while loop as a for loop. This is "ok" but a little clunky, use a for loop for a for loop (you can also use a for loop like a while loop, but there again, its ugly to do so). Checking zero ... its not a special case. I believe the math works fine for zero, and the condition burns as much time as it saves. I think you can take that out entirely. Initialize fraction to 1, and add the terms after that; if the power is zero, youll get 1 correctly I think. Number is misspelled in your final output (looks more like mixed up your languages). You may find unsigned ints to be the best choice for some of the variables. "
may you make an example please?
"I am paranoid, but putting one's name on the web is usually a bad idea. "
you left your name in your code. I was saying, I personally don't like to put my real name out on the web like that.
I will defer to the above for a cleaner version.
note how he factored it to avoid term going out of bounds. This avoids the need to check it, as I had suggested. Good thinking. See also how no zero special case was needed this way. I also forgot about exp, lol.
nice sir i got it, but is hard to follow your way to do it on coding. (my level thinking now is simple to understand such as way to process a problem)
Superb thinking.