Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-24-2003, 04:36 PM   #1
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Boom factorial caclulations in C

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.
Attached Files
File Type: txt ghp6_6.txt (2.5 KB, 41 views)
__________________
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.
Force Flow is offline   Reply With Quote
Old 10-24-2003, 05:31 PM   #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.
Trent Steel is offline   Reply With Quote
Old 10-24-2003, 05:49 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
Attached Images
File Type: jpg formula to calculate e.jpg (4.5 KB, 22 views)
Trent Steel is offline   Reply With Quote
Old 10-25-2003, 01:19 AM   #4
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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.
Force Flow is offline   Reply With Quote
Old 10-25-2003, 02:49 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
Trent Steel is offline   Reply With Quote
Old 10-25-2003, 03:07 AM   #6
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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
Force Flow is offline   Reply With Quote
Old 10-27-2003, 08:35 AM   #7
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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);
}
doctorgonzo is offline   Reply With Quote
Old 10-27-2003, 11:02 AM   #8
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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.
Force Flow is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:08 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2