My Works | Ackerman Function



Ackerman Function: Ackerman function is function with two arguments each of which can be assigned any nonnegative integer, such as 1,2,4,...... This function is defined as follows,
    
     (a) if m = 0, then A(m,n) = n + 1
     (b) if m > 0 but n = 0, then A(m,n) = A(m - 1,1)
     (c) if m > 0 and n > 0, then A(m,n) = A(m - 1,A(m,n - 1))

This function is recursive as it call itself in (b) and (c).

Before getting into code, let see an example using Ackerman Function. Supposing that in this example m = 1 and n = 2. So the function starts as follows,

(01) A(1,2) = A(0,A(1,1)) 'as it falls in condition (c) where m>0 and n>0. Here we got another call to 
                                          'A(1,1). Thus we will have to evaluate it first.
(02) A(1,1) = A(0,A(1,0))  'it also make another call to function A(1,0), so we now will evaluate A(1,0)
(03) A(1,0) = A(0,1)          'it falls in condition (b) and make a call to A(0,1)
(04) A(0,1) = 2                  'it falls in condition (a) and gives a result, 2. Now we can get back to step 
                                          'from where this A(0,1) was called, that is to step (03)
(05) A(1,0) = 2                  'Thus A(1,0) of step (03) gets a value so we van turn back to the step from 
                                          'where this A(1,0) was called that is step (02)
(06) A(1,1) = A(0,2)         'Well, unfortunately it results another call so we will have to do it first
(07) A(0,2) = 3                 'We get this value 3 from the call of step (06)
(08) A(1,1) = 3                 'thus we returned to step (06) and assigned 3 to the caller, that is A(1,1)
(09) A(1,2) = A(0,3)         'now we can move to the caller of A(1,1) at step (01) and it is producing 
                                         'another call to A(0,3), we will do it first
(10) A(0,3) = 4                 'Aaah..it produces a value so we can get back to there from where A(0,3) was 
                                         'called, that is step (09)
(11) A(1,2) = 4                 'Here we assigned the value of A(0,3) to A(1,2) and we are done.

We can easily code this function. We just have to write a function inside which we will put
those above given conditions. Don't think too much and look at the following code,

----------------------------------------------------------
The following downloadable program and code has been found error producing for dig entries, like when m=4 or when m is small but n is bigger. This error is because of Too Many Method Calls or Recursion. I am working on it and I hope that you, the readers will also try to modify it be error free.
----------------------------------------------------------

Function ack(ByVal m As Integer, ByVal n As Integer) As Integer


        If m = 0 Then
            res = n + 1
        ElseIf n = 0 Then
            ack(m - 1, 1)
        Else
            ack(m - 1, ack(m, n - 1))
        End If


        Return res
End Function

Thus, in the above function we transfer our two arguments at m and n, and this function will return the result packed in the variable res. Isn't it so easy to implement !! Look, I haven't wrote a single line of code except the conditions those came with the definition of the Ackerman function.

You can download the project from here, but I hope that you will try it yourself first. Actually I have gave you 99% of the code already, you just have to write a statement which will pass the argument and will call the function and will keep the return to print it on the screen. However, you also download it. [Click Here to Download Ackerman Function]

Remember, I haven't done any kind of exception handling so just don't play with it, ok. Thanks for reading.

Recommended Recommends

Comments

Contact Us

Loading...