Online Mall | Your File Host | Student Loans and Credit | Debt Help | Loans
factorial caclulations in C [Archive] - PCMech Forums

PDA

View Full Version : factorial caclulations in C


Force Flow
10-24-2003, 05:36 PM
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.

Trent Steel
10-24-2003, 06:31 PM
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.

Trent Steel
10-24-2003, 06:49 PM
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

Force Flow
10-25-2003, 02:19 AM
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.

Trent Steel
10-25-2003, 03:49 AM
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

Force Flow
10-25-2003, 04:07 AM
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. :p

Thanks for the help anyway, Trent Steel ;)

doctorgonzo
10-27-2003, 09:35 AM
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:

double factorial(int value)
{
if (!value || (value==1))
{
return 1;
}
return value*factorial(value-1);
}

Force Flow
10-27-2003, 12:02 PM
Thanks, doctorgonzo ;)

I was trying to do that at one time, but it wasn't working right.