|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
I've been hammering away at this problem for several days and haven't been making much headway.
e is Euler's number. e= 1 + 1/2! + 1/3! + 1/4! + ... + 1/(n-1)! + 1/n! The way this prog is supposed to work is to caclulate e with a loop that terminates when the difference between two successive values of e differ by less than 0.0000001. Everytime I rework the problem, it does not work correctly. I have attached what I have. As of now, the loop calculating the *denominator does not work right, and neither does the fact_loop function instead of the loop for *denominator.
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
|
|
|
|
|
|
#2 |
|
Member (10 bit)
Join Date: Jan 2002
Location: Edmonton, AB, Canada
Posts: 628
|
You formula for e is incorrect, using what you have you would approach a limit of 1.718281828 not 2.718281828.
EDIT: See attached pic. Is the correct formula. I have never used C for programing but here is a code that works for matlab, just copy and paste it into a text file and save it as euler.m function [E_new,N] = euler(P) format long N = 0; E_old = 1/FACTORIAL(N); N = N + 1; E_new = 1+1/FACTORIAL(N); while E_new - E_old > P N = N + 1; E_old = E_new; E_new = E_new + 1/FACTORIAL(N); end N E_new The output is N and E_new where N is the number of terms minus one in the corresponding E_new and E_new is the value of e to the input precision which is defined when you call up the function. The input is P which is the desired precision you want e calculated to (eg. 1, 0.1, 0.01 etc). I tested it and it works, I don't know how you would translate it into C though. Last edited by Trent Steel; 10-24-2003 at 05:57 PM. |
|
|
|
|
|
#3 |
|
Member (10 bit)
Join Date: Jan 2002
Location: Edmonton, AB, Canada
Posts: 628
|
This formula works without the built in matlab function of factorial.
function [E_new,N] = euler(P) format long N = 0; D = 1; E_old = 1/(D); N = N + 1; D = N; E_new = 1+1/(D); while E_new - E_old > P N = N + 1; D = D * N; E_old = E_new; E_new = E_new + 1/(D); end N E_new |
|
|
|
|
|
#4 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
That's not quite what I'm trying to do. If I'm reading you last set of equations correctly, that is the whole problem iteself. All I need to do is to figure out how to write a loop that calculates n!. Other than that, everything else in the program is working correctly.
I don't know why I'm having so much trouble with it. Chances are I'm just missing something simplistically stupid. Last edited by Force Flow; 10-25-2003 at 01:22 AM. |
|
|
|
|
|
#5 |
|
Member (10 bit)
Join Date: Jan 2002
Location: Edmonton, AB, Canada
Posts: 628
|
Sorry, the loop in the second post has n! built into it. Each time the loop is run it essentually ads the next term.
function [D] = factorial(N) format long D = 1; while N > 0 D = D * N; N = N - 1; end D This just calculate n!, sorry I can't be more helpful but I don't understand C code |
|
|
|
|
|
#6 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
Yahoo!!! Got it working. It turns out that the "for" loop inside the "while" loop was not needed, in addition to a few mathematical equation adjustments.
heh, you weren't getting my C code, I wasn't entirely getting your mathlab code. ![]() Thanks for the help anyway, Trent Steel
|
|
|
|
|
|
#7 |
|
Professional gadfly
|
I know this is a bit too late to help you, but you may be interested anyway. Computing factorial is a perfect place for a recursive function, i.e. a function that calls itself:
Code:
double factorial(int value)
{
if (!value || (value==1))
{
return 1;
}
return value*factorial(value-1);
}
|
|
|
|
|
|
#8 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
Thanks, doctorgonzo
![]() I was trying to do that at one time, but it wasn't working right. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|