MATLAB Answers

Trouble computing the factorial of a vector

17 views (last 30 days)
Aaron Grandbouche
Aaron Grandbouche on 21 Jan 2021
Edited: John D'Errico on 23 Jan 2021
For a problem I need to get the factorial of numbers 1-50. I started by defining n = [1:50] and then typing factorial(n), but the answer seems really strange, with almost every element equaling to 0 except for the last 4.
Columns 1 through 18
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 19 through 36
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
etc.

  0 Comments

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 21 Jan 2021
Edited: John D'Errico on 23 Jan 2021
No. Look CAREFULLY at that vector.
Are they really zero? Or are they just relatively zero?
F = factorial(1:50);
F([1 2 48 49])
ans =
1.0e+62 *
0.0000 0.0000 0.1241 6.0828
That was using the default display format. Do you see that 1.0e62* in front? What do you think that means? The ENTIRE vector as displayed is multiplied by 1e62, but SOME of those numbers are too small to display when the vector is displayed in this format. So they appear as 0.0000. Had you used a different display format, they would now show up.
format long g
F([1 2 49 50])
ans =
1 2 6.08281864034268e+62 3.04140932017134e+64

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by