1. Tim Holt
  2. Ackermann

Source

Ackermann / Ackermann.cpp

// Ackermann - A simple cpp implementation of the Ackermann–Péter function.
//
// Written in 2014 by Tim Holt (atimholt@gmail.com)
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <http://creativecommons.org/publicdomain/zero/1.0/>.

#include <iostream>
#include <string>
#include <regex>
#include <cstdlib>

typedef unsigned long long int IntT;

void PrintUsage()
{
  std::cout << "Program requires two non-negative integer arguments." << std::endl;
}

bool is_number(char* str)
{
  static std::regex is_number( R"(\d+)" );
  return std::regex_match(str, is_number);
}

IntT Ackermann(IntT m, IntT n)
{
  if (m == 0)
    return n + 1;
  if (m > 0 && n == 0)
    return Ackermann( m - 1, 1 );
  // implied:
  // if (m > 0 && n > 0)
  return Ackermann( m - 1, Ackermann( m, n - 1 ) );
}

int main(int argc, char* argv[])
{
  IntT m, n;

  if ( 3 == argc && is_number(argv[1]) && is_number(argv[2]) )
  {
    m = atoi(argv[1]);
    n = atoi(argv[2]);
    std::cout << "Ackermann(" << m << "," << n << "):" << std::endl;
    std::cout << Ackermann(m,n) << std::endl;
  }
  else
  {
    PrintUsage();
    return 0;
  }

  return 0;
}