### Tags: calculate, factorial, function, hou, infinityvalue, matlab, number, numbers, programming, tryingto

# factorial of a number

On Programmer » Matlab

8,394 words with 8 Comments; publish: Wed, 07 May 2008 10:42:00 GMT; (20060.55, « »)

hi, hou do u calculate factorial of large numbers. when i am trying

to do so using the function "factorial" it is giving me a infinity

value.

waiting for u r reply

*http://matlab.todaysummary.com/q_matlab_16856.html*

All Comments

Leave a comment...

- 8 Comments
- matsi wrote:
<Summary: The OP is having problems calculating factorials of very large

numbers in MATLAB.>

You're probably trying trying to calculate the factorial of a number

where the result is larger than can be represented in MATLAB

double-precision arithmetic. (factorial(170), if memory serves.)

Try using Stirling's Series or Stirling's Approximation. Can't really

enter it very well using an ASCII newsreader, but see

http://mathworld.wolfram.com/Stirli...oximation.html,

or any good book on mathematical physics. (e.g. Arfken & Weber,

"Mathematical Methods for Physicists", Section 10.3)

HTH.

Bob Pownall

#1; Wed, 07 May 2008 10:43:00 GMT

- matsi wrote:
- In article <ef2b07a.-1.matlab.todaysummary.com.webx.raydaftYaTP>, matsi <siva.itsme.matlab.todaysummary.com.gmail.com> wrote
:

> hi, hou do u calculate factorial of large numbers. when i am trying

> to do so using the function "factorial" it is giving me a infinity

> value.

> waiting for u r reply

If it exceeds realmax == 1.7977e+308, then you cannot

represent the number in double precision arithmetic.

However, its also rarely true that you really need the

factorial of a number that large. Most of the time

you can rearrange your computations to avoid it. In a

binomial expansion for example, the coefficients will

usually have a lot of cancellation that one can exploit.

Or if this is not an option, move into the log domain.

Remember that factorial(n) = gamma(n+1). But you don't

need to compute gamma of a large argument and then log

the result. Just use gammaln directly.

gammaln(100000)

ans =

1.0513e+06

Finally, if you have the symbolic toolbox, vpa is

available.

HTH,

John D'Errico

The best material model of a cat is another, or preferably the same, cat.

A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.

Anonymous

#2; Wed, 07 May 2008 10:44:00 GMT

- In article <ef2b07a.-1.matlab.todaysummary.com.webx.raydaftYaTP>, matsi <siva.itsme.matlab.todaysummary.com.gmail.com> wrote
- hi John,
I am really thankful for u r reply. But when I am just trying to

test the function for smaller values like,3! which should be 6,but

gammaln is giving me some different value. can u plz tell me,whether

i can use this function only for larger values.

John D'Errico wrote:

>

> In article <ef2b07a.-1.matlab.todaysummary.com.webx.raydaftYaTP>, matsi

> <siva.itsme.matlab.todaysummary.com.gmail.com> wrote:

>

> trying

> infinity

> If it exceeds realmax == 1.7977e+308, then you cannot

> represent the number in double precision arithmetic.

> However, its also rarely true that you really need the

> factorial of a number that large. Most of the time

> you can rearrange your computations to avoid it. In a

> binomial expansion for example, the coefficients will

> usually have a lot of cancellation that one can exploit.

> Or if this is not an option, move into the log domain.

> Remember that factorial(n) = gamma(n+1). But you don't

> need to compute gamma of a large argument and then log

> the result. Just use gammaln directly.

> gammaln(100000)

> ans =

> 1.0513e+06

> Finally, if you have the symbolic toolbox, vpa is

> available.

> HTH,

> John D'Errico

> --

> The best material model of a cat is another, or preferably the

> same, cat.

> A. Rosenblueth, Philosophy of Science, 1945

> Those who can't laugh at themselves leave the job to others.

> Anonymous

>

#3; Wed, 07 May 2008 10:45:00 GMT

- hi John,
- matsi:
<SNIP expressing some difficulties following <john

d'errico>'s great reasoning...

> But when I am just trying to test the function for smaller values

like,3! which should be 6,but gammaln is giving me some different

value...

well, yes, hopefully so, but

[factorial(3),gamma(3+1)

log(factorial(3)),gammaln(3+1)]

us

#4; Wed, 07 May 2008 10:46:00 GMT

- matsi:
- "matsi" <siva.itsme.matlab.todaysummary.com.gmail.com> wrote in message
news:ef2b07a.2.matlab.todaysummary.com.webx.raydaftYaTP...

> hi John,

> I am really thankful for u r reply. But when I am just trying to

> test the function for smaller values like,3! which should be 6,but

> gammaln is giving me some different value. can u plz tell me,whether

> i can use this function only for larger values.

gammaln will give you the log of the answer, not the answer itself. . But

exp(gammaln(3+1)) will give you something very, very close to 6. It's not

exact because of the issues in dealing with double precision arithmetic.

The usefulness of this approach is apparent when doing things like binomial

expansions, as John suggested. The value nchoosek(n,k) is

factorial(n)/factorial(n-k)/factorial(k). This doesn't work in Matlab if

n>170, even though the final answer might be well within the realm of double

precision math. The solution is this:

exp(gammaln(n+1)-gammaln(n-k+1)-gammaln(k+1)) The main problem, as alluded

to above, is that this may not give an exact answer, but it will be within

machine precision. If the answer is less than 2^52, then rounding it to the

nearest integer should give an exact answer.

This is just an example--in fact, there are better ways of doing that

particular problem (look at the code for Matlab's nchoosek function). But

it shows the basic idea of how to work in the log space.

-Paul

#5; Wed, 07 May 2008 10:47:00 GMT

- "matsi" <siva.itsme.matlab.todaysummary.com.gmail.com> wrote in message
- hi paul,
I Just got what I needed. Thanks for so breifly explaining

me.

Paul Skoczylas wrote:

>

> "matsi" <siva.itsme.matlab.todaysummary.com.gmail.com> wrote in message

> news:ef2b07a.2.matlab.todaysummary.com.webx.raydaftYaTP...

to

> 6,but

> me,whether

> gammaln will give you the log of the answer, not the answer itself.

> . But

> exp(gammaln(3+1)) will give you something very, very close to 6.

> It's not

> exact because of the issues in dealing with double precision

> arithmetic.

> The usefulness of this approach is apparent when doing things like

> binomial

> expansions, as John suggested. The value nchoosek(n,k) is

> factorial(n)/factorial(n-k)/factorial(k). This doesn't work in

> Matlab if

> n>170, even though the final answer might be well within the

realm

> of double

> precision math. The solution is this:

> exp(gammaln(n+1)-gammaln(n-k+1)-gammaln(k+1)) The main problem, as

> alluded

> to above, is that this may not give an exact answer, but it will be

> within

> machine precision. If the answer is less than 2^52, then rounding

> it to the

> nearest integer should give an exact answer.

> This is just an example--in fact, there are better ways of doing

> that

> particular problem (look at the code for Matlab's nchoosek

> function). But

> it shows the basic idea of how to work in the log space.

> -Paul

>

>

>

#6; Wed, 07 May 2008 10:48:00 GMT

- hi paul,
- John D'Errico wrote:
<Summary: Use the gammaln function>

Huh - didn't know about that one. I assume it uses Stirling's Series

for ln(z!).

You learn something new every day...

Bob Pownall

#7; Wed, 07 May 2008 10:49:00 GMT

- John D'Errico wrote:
- In article <120omrppjer04eb.matlab.todaysummary.com.corp.supernews.com>, Bob Pownall <repownall.matlab.todaysummary.com.nets
cape.net> wrote:

> John D'Errico wrote:

> <Summary: Use the gammaln function>

> Huh - didn't know about that one. I assume it uses Stirling's Series

> for ln(z!).

Nope, but you can edit gammaln to see what it does use.

TMW was nice enough to supply an m-file that embodies

the full algorithm for documentary purposes, as well as

the compiled code.

John

The best material model of a cat is another, or preferably the same, cat.

A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.

Anonymous

#8; Wed, 07 May 2008 10:50:00 GMT

- In article <120omrppjer04eb.matlab.todaysummary.com.corp.supernews.com>, Bob Pownall <repownall.matlab.todaysummary.com.nets