EngWiPy
- 1,361
- 61
Hello,
I want to write a recursive function in Mathematica, how can I do that?
Regards
I want to write a recursive function in Mathematica, how can I do that?
Regards
Дьявол said:
Дьявол said:You would write it, for ex. 40!
See the Wolfram Mathematica Help for other recursive functions.
saeddawoud said:I want to write a recursive function in Mathematica, how can I do that?
f[0] := 1
f[n_] := n f[n-1]
CRGreathouse said:Very simple example:
Code:f[0] := 1 f[n_] := n f[n-1]
function Output = ExpanFact(k,j,N_A)
Output=0;
if k==0
Output=1;
elseif j==1
Output = 1/factorial(k);
elseif k==1
Output=j;
elseif j > 0
for l=(k-N_A+1):k
if l >= 0 & l <= (j-1)*(N_A-1)
Output=Output+(ExpanFact(l,j-1,N_A)/factorial(k-l));
end
end
end
output = 0
B[k_, j_, N_] := If[k == 0, output = 1,
If[j == 1, output = 1/Factorial[k],
If[k == 1, output = j,
If[j > 0,
For[l = (k - N + 1), l <= k, l++,
If[l >= 0 && l <= ((j - 1)*(N - 1)),
output = output + B[l, (j - 1), N]/Factorial[k - l],
output]]]]]]
saeddawoud said:Did I miss something?
B[0, j_, N_] := 1
B[1, j_, N_] := j
B[k_, 1, N_] := 1/k!
B[k_, j_, N_] := Sum[B[l, j - 1, N]/(k - l)!, {Max[(k - N + 1), 0], Min[k, (j - 1)*(N - 1)]}]
CRGreathouse said:Well, you're certainly not doing it in the Mathematica style like my factorial example. I would suggest something like
Tell me how that works.Code:B[0, j_, N_] := 1 B[1, j_, N_] := j B[k_, 1, N_] := 1/k! B[k_, j_, N_] := Sum[B[l, j - 1, N]/(k - l)!, {Max[(k - N + 1), 0], Min[k, (j - 1)*(N - 1)]}]
In[1]:= B[0, j_, N_] := 1
B[1, j_, N_] := j
B[k_, 1, N_] := 1/k!
B[k_, j_, N_] :=
Sum[B[l, j - 1, N]/(k - l)!, {Max[(k - N + 1), 0],
Min[k, (j - 1)*(N - 1)]}]
B[3, 3, 3]
During evaluation of In[1]:= Sum::write: Tag Max in Max[3-3+1,0] is \
Protected. >>
Out[5]= (3 Min[2, l])/((3 - l)! l!)
fun[m_, n_, r_] := If [m <= r <= n, 1, 0]
B[0, j_, N_] := 1;
B[1, j_, N_] := j;
B[k_, 1, N_] := 1/k!;
B[k_, j_, N_] :=
Sum[B[l, j - 1, N]/Factorial[k - l]*fun[0, ((j - 1)*(N - 1)), l], {l,
l = k - N + 1, k}]