Advertisement
pakuula

ackerman_iterative.java

Dec 12th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | Science | 0 0
  1. import java.util.Arrays;
  2.  
  3. /**
  4.  * This is the main class for demonstrating the Ackerman function both
  5.  * recursively and iteratively.
  6.  *
  7.  * The Ackerman function is a well-known example of a recursive function that is
  8.  * not primitive recursive.
  9.  *
  10.  * The main method runs a nested loop to compute and prlong the Ackerman function
  11.  * values for a range of inputs.
  12.  *
  13.  * Methods:
  14.  * - main(String[] args): The entry polong of the program. It runs a nested loop
  15.  * to compute and prlong the Ackerman function values.
  16.  * - Ackerman(int i, long n): Computes the Ackerman function recursively.
  17.  * - AckermanIter(int i, long n): Computes the Ackerman function iteratively.
  18.  *
  19.  * @see <a href="https://en.wikipedia.org/wiki/Ackermann_function">Ackermann function</a>
  20.  * @author pakuula@github.com
  21.  * @version 1.0
  22.  * @since 2024-12-12
  23.  */
  24. public class Main {
  25.     /**
  26.      * Computes the Ackermann function iteratively.
  27.      *
  28.      * The Ackermann function is a well-known example of a recursive function that
  29.      * is not primitive recursive.
  30.      * This implementation uses an iterative approach to compute the function.
  31.      *
  32.      * The algorithm uses two arrays, each are of size i+1. The number of loops is
  33.      * i*A(i,n).
  34.      *
  35.      * The algorithm is based on Jerrold W. Grossman and R.Suzanne Zeitman "An
  36.      * inherently iterative computation of ackermann's function".
  37.      *
  38.      * @see <a href="https://doi.org/10.1016%2F0304-3975%2888%2990046-1">An
  39.      *      inherently iterative computation of ackermann's function (1988)</a>
  40.      * @param i the first parameter of the Ackermann function.
  41.      * @param n the second parameter of the Ackermann function.
  42.      * @return the result of the Ackermann function for the given parameters.
  43.      */
  44.     public static long AckermanIter(int i, long n) {
  45.         var vals = new long[i + 1];
  46.         Arrays.fill(vals, 0);
  47.         var goal = new long[i + 1];
  48.         Arrays.fill(goal, 1);
  49.         goal[i] = -1;
  50.        
  51.         while (true) {
  52.             var value = vals[0] + 1;
  53.             for (int j = 0; j < i + 1; j++) {
  54.                 var v = vals[j];
  55.                 vals[j] += 1;
  56.                 if (v == goal[j]) {
  57.                     goal[j] = value;
  58.                 } else {
  59.                     break;
  60.                 }
  61.             }
  62.             if (vals[i] == n + 1) {
  63.                 return value;
  64.             }
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * Computes the Ackermann function, a well-known example of a recursive function
  70.      * that is not primitive recursive.
  71.      *
  72.      * @param i the first parameter, a non-negative longeger
  73.      * @param n the second parameter, a non-negative longeger
  74.      * @return the result of the Ackermann function for the given parameters
  75.      */
  76.     public static long Ackerman(int i, long n) {
  77.         if (i == 0) {
  78.             return n + 1;
  79.         }
  80.         if (n == 0) {
  81.             return Ackerman(i - 1, 1);
  82.         }
  83.         return Ackerman(i - 1, Ackerman(i, n - 1));
  84.     }
  85.  
  86.    
  87.     public static void main(String[] args) {
  88.         for (int i = 0; i < 4; i++) {
  89.             for (long n = 0; n < 5; n++) {
  90.                 System.out.println("(" + i + ", " + n + "): Recursive " + Ackerman(i, n) +
  91.                         ",\tIterative " + AckermanIter(i, n));
  92.  
  93.             }
  94.         }
  95.     }
  96. }
Tags: ackerman
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement