- #1
EngWiPy
- 1,368
- 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]
[COLOR="Red"]During evaluation of In[1]:= Sum::write: Tag Max in Max[3-3+1,0] is \
Protected. >>[/COLOR]
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}]
A recursive function in Mathematica is a function that calls itself in its own definition. This allows the function to be applied repeatedly on smaller and smaller inputs until a base case is reached.
To define a recursive function in Mathematica, you can use the :=
(SetDelayed) operator and include a call to the function within its own definition. Make sure to include a base case to prevent infinite recursion.
The main difference between a recursive function and an iterative function is that a recursive function calls itself in its definition, while an iterative function uses loops to repeatedly apply a set of operations.
Yes, recursive functions in Mathematica can handle large inputs as long as the function is properly defined with a base case to prevent infinite recursion. However, using large inputs may result in longer computation times.
Recursive functions are often used in tasks that involve dividing a problem into smaller sub-problems, such as sorting algorithms, tree traversal, and mathematical sequences. They can also be used for tasks that involve repetitive operations, such as finding the factorial of a number or calculating Fibonacci numbers.