Factorial without the Command

51 visualizaciones (últimos 30 días)
Sophie Culhane
Sophie Culhane el 17 de Sept. de 2020
Comentada: Asad (Mehrzad) Khoddam el 17 de Sept. de 2020
I am struggling to figure out how to compute a factorial without the use of the command. My task is to input a nonnegative integer and have the program compute its factorial.
Here is what I have for my script so far.
function Factorial = ex1(n)
%
%
if n = 0
Factorial = 1;
elseif n >= 1
Factorial = n(n-1)(n-2)(n-3)(3)(2)(1);
end
This program does not run, however I do not know where to go from here.

Respuestas (4)

James Tursa
James Tursa el 17 de Sept. de 2020
Editada: James Tursa el 17 de Sept. de 2020
You could use either use a loop to multiply all of the numbers from 1 to n, or use recursion to multiply n by the factorial of n-1 (with some method of stopping the recursion). Were you instructed to use one of these methods? You could also use some different MATLAB functions for this, but I am not sure which ones would be allowed for your homework.
  1 comentario
Sophie Culhane
Sophie Culhane el 17 de Sept. de 2020
We are only allowed to use very basic commands. (this includes if construct, and the while loop)

Iniciar sesión para comentar.


Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam el 17 de Sept. de 2020
function f = Factorial(n)
%
%
if n = 0 || n = 1
f = 1;
elseif n >= 1
f = n * Factorial(n-1);
end
  1 comentario
Rik
Rik el 17 de Sept. de 2020
Editada: Rik el 17 de Sept. de 2020
Why did you post this answer? Direct answers without an explanation encourages cheating.
If you don't respond I will delete this answer.

Iniciar sesión para comentar.


William
William el 17 de Sept. de 2020
function f = Factorial(n)
%
if n = 0 || n = 1
f = 1;
elseif n >= 1
a = 1:n;
f = prod(a);
end
  4 comentarios
Rik
Rik el 17 de Sept. de 2020
A comment posted after you posted your answer confirmed my suspicion. If you thought this wasn't a homework assignment, why didn't you post the best solution: using the built-in factorial function.
The link you posted did not look like homework to me, although I see how it might be. That user also showed willingness to put in their own effort, and they posted that a hint would also suffice. If that one is a homework question, it is a complex one, and getting help here is a valid strategy.
I would question the usefulness of posting several answers on what is obviously a homework question, if only because the OP has posted a statement to that effect.
I have the option to delete answers, but I chose to not use it if there is at least the suggestion that reasonable people might disagree. If you actually think it isn't a homework question, then you should at least try to answer the question such that you use optimal code.
PS I'll probably move your comment and this one to your answer, because that is what we are discussing.
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam el 17 de Sept. de 2020
From the initial code, I thoughed that he wants to use a recursive function. So I modified the second part of the code. Later after seeing your comment and the code of the others, I noticed that he wants to use basic operations. That's all I did and thank you for your comment about being more carefull.

Iniciar sesión para comentar.


Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam el 17 de Sept. de 2020
Using only basic operations:
function f = Factorial(n)
%
% initial value, 0! and 1!
f = 1;
if n > 1
for i=2:n
f = f * i;
end
end
  1 comentario
Stephen23
Stephen23 el 17 de Sept. de 2020
The if statement is superfluous: that for loop will only iterate for n>=2 anyway.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by