Esempio n. 1
0
int ackerman(int m, int n)
{
  if (m == 0)
    return n+1;
  if (n == 0)
    return ackerman(m-1, 1);
  return ackerman(m-1, ackerman(m, n-1));
}
Esempio n. 2
0
uint64_t ackerman(uint64_t m,uint64_t n){
  if(m==0){
    return n+1;
  } else if(n == 0){
    return ackerman(m-1,1);
  } else {
    return ackerman(m-1,ackerman(m,n-1));
  }
}
Esempio n. 3
0
int ackerman(int m, int n)
{
   if(m==0)
     return n+1;
   else
     if(m>0 && n==0)
       return ackerman(m-1, 1);
     else
       return ackerman(m-1, ackerman(m, n-1));    
}
Esempio n. 4
0
int ackerman(int a, int b) {
/* This is the implementation of the Ackerman function. The function itself is very
   function is very simple (just two recursive calls). We use it to exercise the
   memory allocator (see "my_alloc" and "my_free"). 
   For this, there are additional calls to "gettimeofday" to measure the elapsed
   time.
 */

  char * mem;

  /* The size "to_alloc" of the region to allocate is computed randomly: */
  int to_alloc =  ((2 << (rand() % 19)) * (rand() % 100)) / 100;
  if  (to_alloc < 4) to_alloc = 4;

  int result = 0, i;

  char c;

  mem = my_malloc(to_alloc * sizeof(char));

  num_allocations++;

  if (mem != NULL) {

    // generate a random byte to fulfill the allocated block of memory
    c = rand() % 128;
    memset(mem, c, to_alloc * sizeof(char));

    if (a == 0)
      result = b + 1;
    else if (b == 0)
      result = ackerman(a - 1, 1);
    else
      result = ackerman(a - 1, ackerman(a, b - 1) );

    // check memory value before deleting
    for (i = 0; i < to_alloc; i++) {
        if (mem[i] != c) {
            printf("Memory checking error!\n");
            break;
        }
    }

    my_free(mem);
  }

  return result;
}
Esempio n. 5
0
int main()
{
	//stack will be over flow 
	int a = ackerman(4,3);

	return 0;
}
Esempio n. 6
0
bool test_ackerman(T ackerman, bool logEvents=false)
{
    struct{
        int m;
        int n;
        int ref;
        int _pad;
    }aTests[]={
        {0,0,1},
        {1,0,2},
        {2,0,3},
        {3,0,5},
        {4,0,13},
        //{5,0,65533},
        {0,1,2},
        {1,1,3},
        {2,1,5},
        {3,1,13},
        //{4,1,65533},
        {0,2,3},
        {1,2,4},
        {2,2,7},
        {3,2,29},
        {0,3,4},
        {1,3,5},
        {2,3,9},
        {3,3,61},
        {0,4,5},
        {1,4,6},
        {2,4,11},
        {3,4,125}
    };
    int nTests=sizeof(aTests)/sizeof(aTests[0]);

    int failed=0;
    for(int i=0; i<nTests; i++){
        int n=aTests[i].n;
        int m=aTests[i].m;
        int ref=aTests[i].ref;

        if(logEvents){
            printf("ackerman, n=%u, start\n", i);
        }
        int got=ackerman(m,n);
        if(logEvents){
            printf("ackerman, n=%u, finish\n", i);
        }
        //printf("ack(%d,%d)=%d, got=%d\n", m,n,ref, got);
        if(got!=ref){
            printf("FAIL : ack(%d,%d)=%d, got=%d\n", m,n,ref, got);
            failed++;
        }
    }

    return failed==0;
}
Esempio n. 7
0
int harness_ackerman(T ackerman)
{
    int acc=0;
    for(int n=0;n<4;n++){
        for(int m=0;m<4;m++){
            acc+=ackerman(m,n);
        }
    }
    return acc;
}
Esempio n. 8
0
int main(void)
{
  int i, j;

  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      printf("ackerman(%d, %d)=%d\n", i, j, ackerman(i, j));

  return 0;
}
Esempio n. 9
0
extern void ackerman_main() {
  /* This is function repeatedly asks the user for the two parameters
     "n" and "m" to pass to the ackerman function, and invokes the function.
     Before and after the invocation of the ackerman function, the 
     value of the wallclock is taken, and the elapsed time for the computation
     of the ackerman function is output.
  */

  int n, m; /* Parameter for the invocation of the Ackerman function. */ 

  struct timeval tp_start; /* Used to compute elapsed time. */
  struct timeval tp_end;

  for (;;) {

    num_allocations = 0;

    printf("\n");
    printf("Please enter parameters n and m to ackerman function.\n");
    printf("Note that this function takes a long time to compute,\n");
    printf("even for small values. Keep n at or below 3, and m at or\n");
    printf("below 8. Otherwise, the function takes seemingly forever.\n");
    printf("Enter 0 for either n or m in order to exit.\n\n");

    printf("  n = "); scanf("%d", &n);
    if (!n) break;
     printf("  m = "); scanf("%d", &m);
    if (!m) break;

    printf("      n = %d, m = %d\n", n, m);

    assert(gettimeofday(&tp_start, 0) == 0);
    /* Assert aborts if there is a problem with gettimeofday.
       We rather die of a horrible death rather than returning
       invalid timing information! 
    */

    int result = ackerman(n, m);

    assert(gettimeofday(&tp_end, 0) == 0);
    /* (see above) */

    printf("Result of ackerman(%d, %d): %d\n", n, m, result); 

    printf("Time taken for computation : "); 
    print_time_diff(&tp_start, &tp_end);
    printf("\n");

    printf("Number of allocate/free cycles: %lu\n\n\n", num_allocations); 
  }
  
  printf("Reached end of Ackerman program. Thank you for using it.\n");

}
Esempio n. 10
0
int ackerman(int n, int m)
{
	if (n == 0)
	{
		return 1;
	}

	if (n == 1 && m == 0)
	{
		return 2;
	}

	if (n >= 2 && m == 0)
	{
		return n+2;
	}

	if (n >=1 && m >= 1)
	{
		return ackerman(ackerman(n - 1, m), m - 1);
	}
}