general algorithm + ackermann question

J

jason.s.turner

Hi, I am working on an assignment and I'm a bit confused and stuck.

I am to write an Ackermann function using recursion which was no
problem and dynamic version as well. The dynamic version is giving me
crap.

1st off according to this Wikipedia article,
http://en.wikipedia.org/wiki/Recursion#Functional_recursion

"A famous recursive function is the Ackermann function which, unlike
the Fibonacci sequence, cannot be expressed without recursion."

2nd thing, can a function be dynamic, yet also be recursive? Example:

int ackDyn(int m, int n)
{

switch (m)
{
case 0:
return n + 1;
case 1:
return n + 2;
case 2:
return 2 * n + 3;
case 3:
return (int)Math.Pow(2, n + 3) - 3;
default:
if (n == 0)
{
return ackDyn(m - 1, 1);
}
else
{
return ackDyn(m - 1, ackDyn(m, n - 1));
}
}

}


I attempted to rework it with no self-calls, based on some info that I
found on the Ackerman function combined with the code above, which
resulted in:

long dynAck(long m, long n)
{
long result = 2;
long stop = n + 3;
switch (m)
{
case 0:
return n + 1;
case 1:
return n + 2;
case 2:
return 2 * n + 3;
case 3:
return (int)Math.Pow(2, n + 3) - 3;
default:
if (n == 0)
{
stop = 4;
}
for (int i = 1; i < stop; i++)
{
result = (long)Math.Pow(2, result);
}
return result - 3;
}

}


It seems to almost work, but not quite.

P.S. I'm not using C++, I'm using C#, but I'm not really having
language/platform issues....yet ;)

Thanks,

Jason
 
J

Jack Klein

Hi, I am working on an assignment and I'm a bit confused and stuck.

You certainly are.
P.S. I'm not using C++, I'm using C#, but I'm not really having
language/platform issues....yet ;)

Either you are having an algorithm issue, which belongs in a group
like or you are having a language problem with
C#, which is not C++ and has nothing to do with C++, so belongs in a
group with "dotnet" in its name.

But if you ever do have an actual question about the actual C++
language, please give us another shot.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

jason.s.turner

You certainly are.


Either you are having an algorithm issue, which belongs in a group
like or you are having a language problem with
C#, which is not C++ and has nothing to do with C++, so belongs in a
group with "dotnet" in its name.

But if you ever do have an actual question about the actual C++
language, please give us another shot.

--
Jack Klein
Home:http://JK-Technology.Com
FAQs for
comp.lang.chttp://c-faq.com/
comp.lang.c++http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Sorry about the post....just posted to comp.programming. Thanks
anyway.

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top