Weird Recursive Function :: C++

Status
Not open for further replies.
Hi.

I am having trouble getting a recursive function to work right.

Here is the criteria:

-----
Variables: X, Y

if X == 0
return 2X

if X>= 1 && Y == 0
return 0;

if X >= 1 && Y == 1
return 2;

if X >= 1 && Y >= 2
return return calculateAck(X, calculateAck(X, Y - 1)
-----

Here is the code:
-----
Code:
int CAckermann::calculateAck(int randNumX, int randNumY)
{
   if (randNumX == 0)
      return (2 * randNumY);

   else if (randNumX >= 1 && randNumY == 0)
      return 0;

   else if (randNumX >= 1 && randNumY == 1)
      return 2;

   else if (randNumX >= 1 && randNumY >= 2)
      return calculateAck(randNumX - 1, calculateAck(randNumX, randNumY - 1));
}
-----

The code above works except when it executes the last else-if statement.

Am I doing something wrong? Do I need to deincrement one of the variables before passing it into a function?

Thanks,
Kuphryn
 
Originally posted by kuphryn:

Here is the code:
-----
Code:
int CAckermann::calculateAck(int randNumX, int randNumY)
{
   if (randNumX == 0)
      return (2 * randNumY);

   else if (randNumX >= 1 && randNumY == 0)
      return 0;

   else if (randNumX >= 1 && randNumY == 1)
      return 2;

   else if (randNumX >= 1 && randNumY >= 2)
      return calculateAck(randNumX - 1, calculateAck(randNumX, randNumY - 1));
}

I didn't bother to actually see if that works, I'm just going to get my refactoring on and maybe that'll expose the problem...

Okay, so aside from the first conditional, they all have test for randNumX >= 1 so let's just take care of that upfront. And we'll also add safety braces.

Code:
int CAckermann::calculateAck(int randNumX, int randNumY)
{
    if (randNumX == 0) {
        return (2 * randNumY);
    } else if (randNumX >= 1) {
        if (randNumY == 0) {
            return 0;
        } else if (randNumY == 1) {
            return 2;
        } else if (randNumY >= 2) {
            return calculateAck(randNumX - 1, calculateAck(randNumX, randNumY - 1));
        }
    }
}

Hum. I see a problem. What if randNumX is < 0? There's nothing to
return.

Oh and...
Code:
if (randNumX == 0)
      return (2 * randNumY);
Shouldn't that be "return 2 * randNumX" (or "return 0")

~Kevin
:)
 

DrPizza

Well-known member
93,870

  1. Don't use <tt>int</tt>s, use <tt>unsigned</tt> numbers. Ackermann's function is meaningless for negative inputs, so don't let it have any.
  2. The function will overflow -- that's rather the point of it.
  3. It should look like this:
    Code:
    unsigned long ack(unsigned long x, unsigned long y)
    {
    	if(0 == x) return y + 1;
    	else if(0 == y) return ack(x - 1, 1);
    	else return ack(x - 1, ack(x, (y - 1)));
    }
    Or, if you prefer:
    Code:
    unsigned long ack(unsigned long x, unsigned long y)
    {
    	return (0 == x) ? y + 1 : (0 == y) ? ack(x - 1, 1) : ack(x - 1, ack(x, (y - 1));
    }
    and that's all -- it doesn't need to be any more complicated than that. You don't need the greater-or-equal tests, because if an unsigned number isn't equal to zero, it's greater than it.
  4. Which brings up another point -- both the pseudo-code and the C++ you have do not describe Ackermann's function. This is readily verified by googling.
 
Originally posted by kuphryn:
Peter: Your code did something different.
My code computed Ackermann's function. Your code did not.

It did not overflow the memory.
It will for suitably large numbers.

However, I stalled the program.
No it didn't. It was just taking an awfully long time to compute. And the value was overflowing, many times over.

I try up to intergers m = 10 and n = 10. It still stalled the system.
I believe that Ackermann(10, 10) is in practical terms non-computable. It's an absurdly big number.
 
For some reason, the formula found via Google does not produce the same answer as the one I use from a book.

For example, the formula found via Google produces this:

(3, 3) = 61;

However, the one from a book produces this:

(3, 3) = 65536

65536 is the correct answer.

Here is a snap of the function where one formula is disabled.

-----
unsigned long CAckermann::calculateAck(unsigned randNumX, unsigned randNumY)
{
if (randNumX == 0)
return (2 * randNumY);

else if (randNumX >= 1 && randNumY == 0)
return 0;

else if (randNumX >= 1 && randNumY == 1)
return 2;

else if (randNumX >= 1 && randNumY >= 2)
return calculateAck(randNumX - 1, calculateAck(randNumX, randNumY - 1));=

/*
if (randNumX == 0)
return randNumY + 1;
else if (randNumY == 0)
return calculateAck(randNumX - 1, 1);
else
return calculateAck(randNumX - 1, calculateAck(randNumX, randNumY - 1));
*/
}
-----

Kuphryn
 
Status
Not open for further replies.