# Euler's Number

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-09-2007
BJtoVisualcC++
Euler's Number
I need help on writing a C++ program to compute Euler's number it being

e= 1/0! + 1/1! + 1/2! + ... + 1/n!

• 06-09-2007
Desolation
Seems like homework to me. Tell us what you've done, what you think you can do, pseudo-code, etc.
• 06-10-2007
Junior89
Its not particularly difficult, you need two functions: 1 - something to compute a factorial (simple), and 2 - something to compute the number. I wrote a program that should be working, but for some reason my computer's rounding everything to ints before it adds them =(

I'm a bit confused why a float var type would be becoming an int ?

Good luck!
• 06-10-2007
CodeMonkey
Code:

`return std::exp(1);`
• 06-10-2007
Thantos
Quote:

Originally Posted by Junior89
Its not particularly difficult, you need two functions: 1 - something to compute a factorial (simple), and 2 - something to compute the number. I wrote a program that should be working, but for some reason my computer's rounding everything to ints before it adds them =(

I'm a bit confused why a float var type would be becoming an int ?

Good luck!

No real need to write a factorial function. In fact doing so would probably slow it down.

When doing this problem the first thing to realize is that n will always be greater then or equal to 0. As such 1/0! will always be done. So it is a good idea to initialize based off of that.

So we create a double to store e and give it the value of 1.0 and create an unsigned int to store the denominator and initialize it to 1 (0! is defined to 1 for this problem).

Walk though the steps for n=3:
n=0: e=1.0 d=1
n=1:
d = d * 1 (1! is 1 * 0!)
e = e + 1.0 / d

n=2:
d = d * 2 (2! is 2 * 1!)
e = e + 1.0 / d

n=3:
d = d * 3 (3! is 3 * 2!)
e = e + 1.0 / d

The higher the value of n the more precision you gain.

I hope you can see now the loop in this problem.
• 06-11-2007
BJtoVisualcC++
So now I just put in a while for some n value?
• 06-11-2007
brewbuck
Quote:

Originally Posted by Junior89
Its not particularly difficult, you need two functions: 1 - something to compute a factorial (simple), and 2 - something to compute the number. I wrote a program that should be working, but for some reason my computer's rounding everything to ints before it adds them =(

I'm a bit confused why a float var type would be becoming an int ?

Good luck!

A factorial function would be inefficient. Notice that the denominator is always the factorial of the next higher number. This means you should just keep a running product and multiply the next factor in on every iteration.
• 06-11-2007
Thantos
Quote:

Originally Posted by BJtoVisualcC++
So now I just put in a while for some n value?

while, for, do while, take your pick

I think I used a for but thats mainly personal preference.
• 06-11-2007
BJtoVisualcC++
what I have
see attachment
• 06-11-2007
dwks
I can't open attachments on this computer, and I'm sure some people don't feel like clicking on the link, either. Since it's only 621 bytes you might as well just post it in [code] tags.
• 06-11-2007
BJtoVisualcC++
Quote:

Originally Posted by Desolation
Seems like homework to me. Tell us what you've done, what you think you can do, pseudo-code, etc.

did you see my attachment
• 06-11-2007
Thantos
Quote:

Originally Posted by BJtoVisualcC++
did you see my attachment

Obviously they didn't since you posted the attachment after they posted.
• 06-11-2007
Thantos
After looking at your code I can see you totally ignored my explanation of the problem.

Let me ask these two questions:
What is the value of e?
Can a variable of type long store the value of e?
• 06-11-2007
Salem
> return factX;
> cout << "The Euler's number is " << factX << endl;
Remove the return, it is stopping you printing.
• 06-11-2007
CodeMonkey
Code:

```#include <iostream> #include <iomanip> #include <string> #include <cmath> using namespace std;  //I would avoid this. If your teacher disagrees, strangle him. int main( ) {   int n;            //Initializing counter      //fine, but for clarity you might want to save it for later   long factX = 1;  //setting factorial to start at 1      //e is not a type of integer! Use double.   n = 0;          //setting counter to zero          //might as well do this when you declare the variable, might as well within the loop   for(n = 1, factX =1; n<= 10; n++)  factX = factX * n;   return factX;  //return to the caller? Uh oh...   cout << "The Euler's number is... doesn't matter because this never executes...  " << endl;   system("pause");   return 0; }```
Just think about what you're telling the computer to do:

1. Allocate space for an integer. Call it "n".

2. Allocate space for a long integer. Call it factX. Initialize its value to the integral constant, 1.

3. Set n to the integral constant, 0.

4. Set n to 1. Set factX to 1. Until n is not less than or equal to 10, execute the below code, each time checking first to see if n is still less than or equal to 10. Increase n by 1 each time, after the below code is executed.

4a (The "below" code): set the value of factX so that it is equal to the product of itself and n (this will be an integer).

5. Now that the loop is done, get ready to return to the system process that called this program and give it the value we were calling factX. Clean up. End execution.

Is this what you want to program to do? Write out in prose what you want done, so that a computer might understand it. Then make code.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last