C++ Algorithm Ackerman's function.

Description

C++ Algorithm Ackerman's function.

#include <iostream>
#include <iomanip>

unsigned long long ack(unsigned long long m,  unsigned long long n);

int main(){//from  w  w w . j  a  v a2  s  .  c o m
  int m {2};
  int n {2};

  // Create array dynamically to hold values
  unsigned long long** ack_values = new unsigned long long*[m + 1];  // Pointer to array of arrays of unsigned long long elements

  for (int i = {}; i <= m; ++i)
    ack_values[i] = new unsigned long long[n + 1];

  // Store values in the array
  for (int i = {}; i <= m; ++i)
    for (int j = {}; j <= n; ++j)
      ack_values[i][j] = ack(i, j);

  for (int i {}; i <= m; i++)
  {
    std::cout << std::endl;
    for (int j {}; j <= n; ++j){
      std::cout << std::setw(12) << ack_values[i][j];
    }
  }
  std::cout << std::endl;

  for (int i {}; i < m + 1; ++i){
    delete[] ack_values[i];
  }
  delete ack_values;
}

unsigned long long ack(unsigned long long m, unsigned long long n){
  if (m == 0ULL)
    return n + 1;
  if (n == 0ULL)
    return ack(m - 1, 1);
  return ack(m - 1, ack(m, n - 1));
}



PreviousNext

Related