E number

/*HEllo everybody. i would like to see your recommendations and your quotes.

how can i optimize this code?
what is wrong into the code?
what can it be improved?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*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;
}
Last edited on
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.
Last edited on
"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. "

Explain me this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>

int main()
{
    std::cout << "computes the value of e^x by using the e^x =  1 + x/1! + x^2/2! + x^3/3! ...\n" ;

    double x ;
    std::cout << "x? " ;
    std::cin >> x ;

    unsigned int num_terms ;
    std::cout << "number of terms? " ;
    std::cin >> num_terms ;

    double result = 1.0 ;
    double term = 1.0 ;
    for( unsigned int n = 1 ; n < num_terms && term != 0.0 ; ++n )
    {
        term *= x ;
        term /= n ;
        result += term ;
    }

    std::cout << std::fixed
              << "computed result: " << result << '\n'
              << " library result: " << std::exp(x) << '\n' ;

    // https://en.cppreference.com/w/cpp/numeric/math/exp
    if( std::exp(x) == HUGE_VAL ) std::cout << "*** range error (value is too large)\n" ;
}
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.
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.