factorial in matlab R2010a

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" ?

More Answers (4)

>> 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

0 votes

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

Test it yourself to convince yourself. I believe it; and I trust John.
>>factorial(vpi(455))/factorial(vpi(448))
>>prod(449:455)
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

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.
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

0 votes

"a" and "b" are integers but the result is "0" a/b must return 1.4881e-005 but its return 0

12 Comments

How can an integer (i.e. something that CAN ONLY BE AN INTEGER) possibly be 1.4881E-5?
double(a) / double(b)
thiiiiiiiinks
double(a)/double(b) is the solution !!
the result is not an integer !!
So then was the problem with vpi() or was the problem with your understanding of how MATLAB does arithmetic operations on integers?
vpi help me to compute factorial of x when x is large but
its true i has a problem with matlab arithmetic operations on integers .
but now its over i can finaly compute the quotient a/b .
Until, that is, the values you work with happen to exceed 10^308 ...
but i have many quotient in my fuctions
at last the result is < 1
And how are you going to handle 360! / 450! ?
euuh i dont know
i'm trying but the result is NAN
Yup. You can use gammaln() as John suggests, but you are going to have problems if the uncancelled integer terms exceed 17!
It seems likely to me that you have the Symbolic Toolbox; if so then you should be switching to it for these calculations.
how i can implemente it in matlab ??
For factorial in the symbolic toolbox, see http://www.mathworks.com/matlabcentral/answers/2662-how-to-perform-symbolic-factorial

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!