Well I did it with two different implementations of the tarai function that I dl'ed from the Internet, and they both give me 18 as the answer too:
Code:
#include <iostream>
#include <cstdlib>
int tarai(int x, int y, int z);
int main(int argc, char * argv[])
{
int myInt = 0;
int int1 = atoi(argv[1]);
int int2 = atoi(argv[2]);
int int3 = atoi(argv[3]);
myInt = tarai(int1, int2, int3);
std::cout << int1 << std::endl;
std::cout << int2 << std::endl;
std::cout << int3 << std::endl;
std::cout << myInt << std::endl;
return 0;
}
/*
int tarai(int x, int y, int z)
{
while (x > y)
{
int oldx = x, oldy = y;
x = tarai(x - 1, y, z);
y = tarai(y - 1, z, oldx);
if (x <= y) break;
z = tarai(z - 1, oldx, oldy);
}
return y;
}
*/
int tarai(int x, int y, int z)
{
if (x <= y) {
return y;
}
else {
return tarai(tarai(x - 1, y, z),
tarai(y - 1, z, x),
tarai(z - 1, x, y));
}
}