ackermann


See On Github

Data

Contributor

Generic placeholder thumbnail

by alsuga

in c++

Tags

Source Code

CC= g++ -std=c++11

all: ackermann

ackermann: ackermann_test.cc
	$(CC) ackermann_test.cc ackermann.cc -o ackermann.out

clean: 
	rm -rf *.out
long long ackermann(long long m, long long n) {
  if(m == 0)
    return n + 1;
  if(n == 0)
    return ackermann(m - 1, 1);
  return ackermann(m - 1, ackermann(m , n - 1));
}
#ifndef ACKER
#define ACKER

long long ackermann(long long m, long long n);

#endif
#include <iostream>
#include <cassert>
#include "ackermann.hpp"

using namespace std;

int main() {
  // based on https://en.wikipedia.org/wiki/Ackermann_function#Table_of_values
  assert(ackermann(0, 2) == 3);
  assert(ackermann(0, 5) == 6);

  assert(ackermann(0, 0) == 1);
  assert(ackermann(2, 0) == 3);
  assert(ackermann(5, 0) == 65533);

  assert(ackermann(3, 4) == 125);
  assert(ackermann(4, 1) == 65533);
  
  cout << "All test passed" << endl;
  return 0;
}