factorial in matlab R2010a

8 views (last 30 days)
stof
stof on 6 Apr 2011
hello! I want to calculate factorial (x) knowing that "x" can be a very large value, sometimes up to 450. factorial (450) = "INF" how to have real value and not "INF" ?

Accepted Answer

Sean de Wolski
Sean de Wolski on 6 Apr 2011

More Answers (4)

John D'Errico
John D'Errico on 6 Apr 2011
>> factorial(vpi(450))
ans =
17333687331126326593447131461045793996778112652090
5101556920750955533300168343675060467508829043871061
4581128451842409785861858380630165020834729618135166
7570171918700422280962237272230663528084038062312369
3426741350366101015088382204949709297390116367937661
6502373085389640390159083614414959443268420451378471
6402303182604094683993315061302563918385303341510606
7614624202058200069363520959674171831915387256175095
2138055678130919542980022927380334255355816459199629
8912368598547771179158461351340068905647127658164836
3771263037749233600780723074620085543550683614481266
0628114576096049918781342839792484059250453784948742
5060488481036571447957046788635742936714615176219148
4697431029799497407144851047161696640523973926028484
0869400740899890112749290517151447343138663339249204
0661522692303043813960541966093224243809225137268851
7179043032140582384479361116785682369730362384046265
0789068800000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000
000000000000000
If you really want to compute the true value, then as Sean says, use vpi. However many times it is not your final goal. Often you want to work in logs anyway. In that case, look at the relationship between the gamma function and factorial, i.e., factorial(n)=gamma(n+1), for integer n. So if you want the natural log of factorial(n), then use gammaln(n+1).
Finally, many times, you don't really need the full factorial, but a binomial coefficient.

stof
stof on 6 Apr 2011
think you so much !! the calculation of factorial(449) is ok but are you sure that this function "VPI" does not affect the result of factorial ??
  2 Comments
Sean de Wolski
Sean de Wolski on 6 Apr 2011
Test it yourself to convince yourself. I believe it; and I trust John.
>>factorial(vpi(455))/factorial(vpi(448))
>>prod(449:455)
John D'Errico
John D'Errico on 6 Apr 2011
I am ABSOLUTELY confident that vpi gives you the correct value for the factorial, to ALL digits of the result. Ok, if you wish to compute a number as large as factorial(1e15), you will still run out of computer long before vpi computes all of those digits, but vpi will compute the exact factorials for numbers with only a few hundred thousand or even a few million digits. You are still advised then to use gammaln, and work in logs for many problems.

Sign in to comment.


stof
stof on 6 Apr 2011
ok i test it ans vpi give me the the real value but i have an other problem in this code: >> a=p(0,449) a = 449 >> b=p(1,449) b = 30172800 >> c=a/b c = 0
but : >> 449/30172800
ans =
1.4881e-005
the fuction vpi does not work in this case !!
  2 Comments
Walter Roberson
Walter Roberson on 6 Apr 2011
Check the data types of a and b. When you do an arithmetic operation with integer data types, the result is returned in the same data type.
John D'Errico
John D'Errico on 6 Apr 2011
It does work, but it only returns the integer part of the ratio, since vpi is an INTEGER format. That part happens to be zero.

Sign in to comment.


stof
stof on 6 Apr 2011
"a" and "b" are integers but the result is "0" a/b must return 1.4881e-005 but its return 0
  12 Comments
stof
stof on 6 Apr 2011
how i can implemente it in matlab ??
Walter Roberson
Walter Roberson on 6 Apr 2011
For factorial in the symbolic toolbox, see http://www.mathworks.com/matlabcentral/answers/2662-how-to-perform-symbolic-factorial

Sign in to comment.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!