problem using factorial in matlab

4 ビュー (過去 30 日間)
Parikshit  Sharma
Parikshit Sharma 2017 年 11 月 8 日
コメント済み: Walter Roberson 2017 年 11 月 9 日
I wanted to calculate 200! but after 170! matlab starts showing result NaN. So is there any way possible to calculate this number using matlab???

採用された回答

John D'Errico
John D'Errico 2017 年 11 月 8 日
編集済み: John D'Errico 2017 年 11 月 8 日
You cannot solve it with a double precision result, because it will overflow the dynamic range of a double.
factorial(200)
ans =
Inf
But if you are willing to use other tools, there is no problem. For example, the symbolic toolbox gives you this:
factorial(sym(200))
ans =
788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000
Or, my own vpi toolbox , found on the file exchange:
factorial(vpi(200))
ans =
78865786736479050355236321393218506229513597768717326329474253324435
944996340334292030428401198462390417721213891963883025764279024263710506
192662495282993111346285727076331723739698894392244562145166424025403329
186413122742829485327752424240757390324032125740557956866022603190417032
406235170085879617892222278962370389737472000000000000000000000000000000
0000000000000000000
In either case, the result is an integer with almost 400 digits.
If you are happy with a floating point number, use my HPF toolbox . So in 50 digits of precision...
factorial(hpf(200,50))
ans =
7.8865786736479050355236321393218506229513597768717e374
Remember that any such high precision computation will be far less efficient than using a double. You are almost always better off using good numerical methods to avoid the problem in the first place. But that is why they teach entire courses about such methods, and books are written on the topic.
  2 件のコメント
Parikshit  Sharma
Parikshit Sharma 2017 年 11 月 9 日
I know this sound stupid but I am very new to matlab and I don't know how to use HPF toolbox suggested by you so will you please elaborate??
Walter Roberson
Walter Roberson 2017 年 11 月 9 日
Click on the link and click on Download. Save to your drive. Unzip into a convenient directory that is not underneath the MATLAB installation directory. At the MATLAB level, use pathtool to add that directory to your path. After that you can call upon the routines John has written.

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 11 月 8 日
If you're computing this as an intermediary step to compute a final result and will immediately divide it by the factorial of some other number, don't compute the result that way. My guess is that you're trying to compute the binomial coefficient nchoosek, in which case you should use that function.
If you can't use nchoosek (because your homework tells you not to, for example) you could turn a ratio of two factorials into the appropriate call to prod by using cancellation. [To see what I mean, write out how you would compute factorial(8) and factorial(4) on paper. Determine how you would use those representations to compute factorial(8)/factorial(4) without actually calling factorial, then generalize that process.]

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by