2

I can't solve 200! with Matlab. When I use the factorial function I get an answer of just inf. How can I find this?

2
  • 1
    Do you need a precise answer or an approximation?
    – wvdz
    Oct 19, 2014 at 18:06
  • 2
    Actually I want to compare stirling's approximation and real result use with matlab for my assignment
    – Fatih
    Oct 19, 2014 at 19:51

3 Answers 3

4

In R2012a, and later, using the Symbolic Math toolbox and sym/factorial:

factorial(sym(200))

which returns the exact value of 200!

788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000

This matches the answer returned by Wolfram Alpha.

3

The factorial of n is given exactly by gamma(n+1), where gamma is Euler's gamma function. The problem is that the actual result in this case exceeds realmax, so gamma(201) outputs inf. To solve that, use variable-precision arithmetic (function vpa, which is part of the Symbolic Toolbox):

>> gamma(vpa(200)+1)
ans =
7.8865786736479050355236321393219*10^374

This computes the result with an accuracy of d decimal digits, where d is set by the function digits (thanks to @horchler for reminding me of that). By default that's 32 digits. To increase that number,

>> digits(60);
>> gamma(vpa(200, 20)+1)
ans =
7.88657867364790503552363213932185062295135977687173263294743*10^374

Alternatively, you could use vpa with the factorial definition:

>> prod(vpa(1:200, 50))
ans =
7.8865786736479050355236321393219*10^374

A third possibility is to directly use factorial(vpa(200)). However, this may not work for old Matlab versions, such as R2010b, in which factorial doesn't seem to accept a symbolic input.

1
  • 1
    FYI, factorial(vpa(200)) works in newer versions. You should indicate that the results produced by vpa are an approximation and depends on the value of digits.
    – horchler
    Oct 19, 2014 at 19:41
2

Approximation:

You can find a value using Stirling's approximation. It is very accurate for large factorials.

The forumla is

ln(n!) = nln(n) - n +O(ln(n))

Precise Answer:

Use this function called fact. It calculates factorial above 170.

Example:

fact(double(200))

Answer: 788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000
2
  • 1
    Looking at the code for that fact function, it seems like it's nothing more than sym/factorial. Since the code appears to be pre-R2012a, I'm guessing that the slightly awkward method used was necessary to evaluate symbolic factorials prior to sym/factorial being explicitly exposed.
    – horchler
    Oct 20, 2014 at 2:33
  • You should probably link to the actual formula, not the relationship: upload.wikimedia.org/math/5/0/0/…
    – rayryeng
    Nov 16, 2014 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.