# Problem with factorial recursive function

38 vues (au cours des 30 derniers jours)
Stu145 le 28 Jan 2015
Whenever I run the code for a matrix of n values, like n=1:10, only the last 2 factorials are displayed while the rest are 0's. Would anyone be able to tell me where I have gone wrong?
Code:
function x = fact(n)
if n<=1
x = 1;
else
x = n .* fact(n-1) ;
end
end
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (5)

Titus Edelhofer le 28 Jan 2015
Hi Arun,
your code does not work for arrays because of the "if n<=1". You would need to do something like
idx = (n>1);
x(~idx) = 1;
x(idx) = n(idx).* ...
Titus
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
John D'Errico le 28 Jan 2015
For how large of a value are you trying to compute the factorial? Be careful, as this could be important, since that too would blow up the recursion limits.
Regardless, as you have written it, the code will still run forever. What happens when all values of n are <= 1?
The function as you wrote it will still call fact, but with an empty array for n. so you will have fact calling fact, each time with an empty argument n.
I would add a test to stop that from happening.
Shubham Tyagi le 15 Août 2016
I'm compiling this code with a 2*4 matrix but (dimensions doesn't match in val(i)=r(i).*fact(r(i)-1);) type error is coming.
function val=fact(r)
i=(r>1); val(~i)=1; if r==1 return end val(i)=r(i).*fact(r(i)-1); end

Connectez-vous pour commenter.

Image Analyst le 31 Jan 2015
Not sure what you did, but it worked fine for me. I put all this (both functions) into test.m
function test
clc;
for k = 1 : 10
kFactorial = fact(k);
fprintf('%d! = %d\n', k, kFactorial);
end
function x = fact(n)
if n<=1
x = 1;
else
x = n .* fact(n-1) ;
end
In the command window you'll see:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
##### 4 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Image Analyst le 15 Août 2016
I don't know what matrix you're talking about. Please give the code you are going to run to call your function, and show what output you'd expect to see.
If you want fact n to print out an array from the first integer to the last integer, the fact() function will need to be modified since now it's just expecting a single number, not a vector or matrix.
Torsten le 25 Août 2016
Reshape everything to vectors and reshape back to matrices, if necessary.
Best wishes
Torsten.

Connectez-vous pour commenter.

Titus Edelhofer le 25 Août 2016
Hi,
two minor changes are needed:
function x = fact(n)
x = ones(size(n));
idx = (n>1);
if any(idx)
x(idx) = n(idx).*fact(n(idx)-1);
end
First, you need to initialize x as an array of the same size as n (because for fact(n-1) otherwise the array could be smaller), giving the error you observed.
Second, protect the recursive call to fact(n-1) by doing this only if there is anything to do...
Titus
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Titus Edelhofer le 26 Août 2016
Calling for a matrix works fine, btw.:
fact([2 3 4; 5 6 7])
ans =
2 6 24
120 720 5040

Connectez-vous pour commenter.

Anil Sarode le 26 Déc 2017
Modifié(e) : Walter Roberson le 26 Déc 2017
sorry i copied your code :
this is the one way to solve your problem :
function file: from where you make function call each time using for loop :
n=1:10;
y=[];
for i=1:length(n)
y(i)=fact(i) ;
end
function [x] = fact(n)
if n<=1
x = 1;
else
x = n .* fact(n-1) ;
end
end
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Abdimalik le 14 Déc 2022
You don't need to read input or print anything. Your task is to complete the function factorial() which takes an integer N as input parameters and returns an integer, the factorial of N.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Logical dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by