MATHEMATICS

Jumat, 07 Oktober 2011

Primitive recursive function

Normally you calculate n factorial with Factorial[n] or short n!. Mathematica handles the details of the function for you and prints the result.

In the course M381 you have to prove that functions like factorial are a primitive recursive function. This basically means that the function can be defined only in terms of itself, add one, or set to zero. A primitive recursive definition of factorial would look as follows in Mathematica.

suc[n1_] := n1 + 1
add[n1_, 0] := n1
add[n1_, n2_] := suc[add[n1, n2 - 1]]
mul[n1_, 0] := 0
mul[n1_, n2_] := add[mul[n1, n2 - 1], n1]
fac[0] := suc[0]
fac[n_] := mul[n, fac[n - 1]]


As you can see no other Mathematica functions than "+ 1" and "= 0" are used. The functions suc, add, mul, fac are defined for the first time.

For example:
In[67]:= Factorial[6]
fac[6]

Out[67]= 720

Out[68]= 720
.

Tidak ada komentar:

Posting Komentar