Thread: Ackerman Function

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    8

    Cool Ackerman Function

    Q. The Ackerman recursion can be described as below: A (0, n)
    = n + 1
    A (m + 1, 0) = A (m, 1)
    A (m + 1, n + 1) = A (m, A (m + 1, n))
    Given m and n as input, write a program to calculate A(m, n), using recursion.

    Code:
    
    #include<stdio.h>
    int Ackerman(int m,int n);
    int main()
    {
        int m,n;
        printf("Enter two numbers: ");
        scanf("%d%d",&m,&n);
        printf("Ackerman(%d,%d) = %d.\n",m,n,Ackerman(m,n));
        return 0;
    }
    int Ackerman(int m,int n)
    {
        if(m==0)
            return n+1;
        if(n==0 && m>0)
            return Ackerman(m-1,1);
        if(m>0 && n>0)
            return Ackerman(m-1,Ackerman(m,n-1));
    }

    Thanks you for your constructive advices.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    679
    Since Ackerman recursion cannot be called with negative values:
    Code:
    unsigned int ackermann(unsigned int m, unsigned int n)
    {
      if (!m) return n + 1;
      if (!n) return ackermann(m - 1, 1);
      return ackermann(m - 1, ackermann(m, n - 1));
    }

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    8
    Thankyou

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-19-2020, 05:50 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

Tags for this Thread