Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.5 KiB

  1. ```
  2. class Ackermann_function
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         //prints intro
  7.         System.out.println("This program will solve for all values in ackermann
  8. function using recursion.1");
  9. System.out.println("**************************n")
  10.         //calls for all values of ackerman using for loop
  11.         for(int i = 0; i < 6; i ++)
  12.         {
  13.             for(int j = 0; j < 10; j ++)
  14.             {
  15.                 System.out.println("Ackerman (" + i + "," + j + ") is: " +
  16. ack(i,j));
  17.             }
  18.         }
  19.         //test sinlge
  20.         //System.out.println(ack(3,1));
  21.     }
  22.     public static int ack(int m, int n)
  23.     {
  24.         if(m == 0)
  25.         {
  26.             return(n + 1);
  27.         }
  28.         else if(m > 0 && n == 0)
  29.         {
  30.             return(ack(m-1,1));
  31.         }
  32.         else if(m >0 && n > 0);
  33.         {
  34.             return(ack(m-1, ack(m,n-1)));
  35.         }
  36.     }
  37. }
  38. ```
  39. The Ackermann function is a classic example of a function that is not primitive
  40. recursive – you cannot solve it using loops like Fibonacci. In other words, you
  41. have to use recursion to solve for values of the Ackermann function.
  42. For more information on the Ackermann function [click
  43. here](https://en.wikipedia.org/wiki/Ackermann_function).