bench mark of compiler using Ackerman function




Recursion
A function may be called from another function or it may call itself. Recursion implies afunction calling itself. If a function call occurs inside the function itself, it is called
 direct recursion
 If a function calls another function, which in turn makes a call to the first one,it is called
indirect recursion
Each invocation of a function causes a new allocation of the variables declared inside it.The storage for auto variables is automatically allocated and freed on function entry andreturn.
Advantages of recursive functions:
Recursive functions are more concise and understandable.
Disadvantages of recursive functions:
Recursive functions are slower than the corresponding iterative functions.
Recursive functions are slow because of the overhead of passing arguments and-returning values.
Recursive functions take up more memory. Each time the function calls itself, the current status of the function must be preserved (the line being executed, thevalues so far). This information is saved onto a data structure called a “stack”. If recursion goes on for a long time, we get the “Stack Overflow” error. So it is-important to give an “end – condition” so that recursion eventually terminates.
Ackermann function
It takes two natural numbers as arguments and yields another natural number. Its value-grows extremely quickly; even for small inputs. For example for (4,3), the values of theAckermann function are so large that they cannot be feasibly computed, and in fact their decimal expansions require more digits than there are particles in the entire visibleuniverse.
Definition and properties 
The Ackermann function is defined recursively for non-negative integers m and n asfollowsAck (0, n) = n + 1 for n >= 0 Ack (m, 0) = Ack (m
−
1, 1) for m >= 1 Ack (m, n) = Ack (m
−
1, Ack (m, n
−
1)) for m >= 1, n >= 1 This may also be represented as:
  
 
The evaluation of these functions always terminates. The recursion is bounded because in-each recursion either m decreases, or m remains the same and n decreases. Each time that n reaches zero, m decreases, so m eventually reaches zero as well. However, when m decreases there is no upper bound on how much n can increase.For small values of m like 1, 2, or 3, the Ackermann function grows relatively slowlywith respect to n. For m
≥
4, however, it grows much more quickly. The expansion of A(4, 3) cannot be recorded in the known physical universe.Computing A(1, n) takes linear time in n. Computing A(2, n) takes quadratic time in n.The time required for higher values of m, is much more.
Explanation 
We now illustrate the expansion of the function for m = 1, and n = 2. Theevaluation of 
 A
(1, 2) proceeds as follows:A(1, 2) = A(0, A(1, 1))= A(0, A(0, A(1, 0)))= A(0, A(0, A(0, 1)))= A(0, A(0, 2))= A(0, 3)= 4

Applications of Ackermann’s function 
The Ackermann function, due to its definition in terms of extremely deep recursion, can be used as a benchmark of a compiler's ability to optimize recursion. This function can beused to compare the amount of time required to evaluate this function for fixed argumentsin many different programming language implementations.

For Understanding : https://en.wikipedia.org/wiki/Ackermann_function
package com.kartik.ackerman;

import java.math.BigDecimal;

public class Ackerman { 
 
 private BigDecimal ack(BigDecimal m,BigDecimal n)
 {
  BigDecimal constant= new BigDecimal(1);
  BigDecimal constantSec= new BigDecimal(0);
  if(m.compareTo(constantSec) == 0 )
   return n.add(constant);
  else if (n.compareTo(constantSec)==0)
   return ack(m.subtract(constant),constant);
  else
   return ack(m.subtract(constant),ack(m,n.subtract(constant)));
 }
 
 public static void main(String[] args) {
  Ackerman ackerman =new Ackerman();
  BigDecimal first = new BigDecimal(3);
  BigDecimal second = new BigDecimal(11);
  BigDecimal result = ackerman.ack(first,second);
  System.out.println(result);

 }
}
OutPut: 16381

How to Algo work?


Previous
Next Post »