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.

86 lines
1.7 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.  
  12.         for(int i = 0; i \< 6; i ++)
  13.         {
  14.             for(int j = 0; j \< 10; j ++)
  15.             {
  16.                 System.out.println("Ackerman (" + i + "," + j + ") is: " +
  17. ack(i,j));
  18.             }
  19.  
  20.         }
  21.         //test sinlge
  22.         //System.out.println(ack(3,1));
  23.     }
  24.     public static int ack(int m, int n)
  25.     {
  26.         if(m == 0)
  27.         {
  28.             return(n + 1);
  29.         }
  30.         else if(m \> 0 && n == 0)
  31.         {
  32.             return(ack(m-1,1));
  33.         }
  34.         else if(m\>0 && n \> 0);
  35.         {
  36.             return(ack(m-1, ack(m,n-1)));
  37.         }
  38.     }
  39.  
  40.  }
  41. \`\`\`
  42. The Ackermann function is a classic example of a function that is not primitive
  43. recursive – you cannot solve it using loops like Fibonacci. In other words, you
  44. have to use recursion to solve for values of the Ackermann function.
  45. For more information on the Ackermann function [click
  46. here](https://en.wikipedia.org/wiki/Ackermann_function).