1

I was watching this video - https://youtu.be/i7sm9dzFtEI on youtube, and decided it would be fun to copy that function in C#

Well I ran it, and it stopped after 21 calls and around one second. The guy's program on the video has apparently been running for about 4 weeks and is still running.

static int ack(int m, int n)
{
    int answer;
    if (m == 0) answer = n + 1;
    else if (n == 0) answer = ack(m - 1, 1);
    else answer = ack(m - 1, ack(m, n - 1));
    return answer;
}
static void Main(string[] args)
{
    for (int i = 0; i < 6; i++)
        for (int j = 0; j < 6; j++)
            Console.WriteLine($"ackerman ({i},{j}) is:{ack(i, j)}");
    Console.ReadLine();
}

Is there something I can do to make mine run for longer?

Edit: I changed from 'Debug' to 'Release' and got one more iteration and a few more seconds.

MrVimes
  • 3,212
  • 10
  • 39
  • 57
  • Are you using the same language as them? Unless your language has "optimized recursion", you won't have a continuously recursing program running for more than a few seconds. – Carcigenicate Sep 27 '17 at 22:07
  • I believe theirs was written in C. I could try rewriting it in C myself. – MrVimes Sep 27 '17 at 22:11
  • IDK if C or C# supports TCO. Now that I look at the code again though, I don't even know if it qualifies for TCO. – Carcigenicate Sep 27 '17 at 22:12
  • this isn't a function that would be very C# friendly. C# is a managed language, with automatic memory management; it's likely that every single instance of this function is in memory until the entire recursion tree has been processed, and with a tree this large, you are essentially running out of memory before the instances are all parsed. – Claies Sep 27 '17 at 22:15
  • in fact, the point of the video is that, eventually, even the most powerful computer would segment fault (stack overflow) with this function. – Claies Sep 27 '17 at 22:17
  • @Claies I understand, but his has been running for four weeks whereas mine lasts a few seconds. It looks like his is running on his desktop computer but I could be wrong. I will try rewriting it in C and see what happens. – MrVimes Sep 27 '17 at 22:21
  • If you look at the [Table of Values, Wikipedia](https://en.wikipedia.org/wiki/Ackermann_function#Table_of_values) you'll see that independent of the number of calls, the _values_ computed quickly exceed that which can be computed using 32-bit or even 64-bit arithmetic. So, without watching the video to learn why he points to that C program, I think the C program is just meant to be an _example_ of how to compute Ackerman's function - not a _working implementation_ which would need bignum arithmetic. In C# you could try replacing your ints with BigInteger and see how far you get. – davidbak Sep 27 '17 at 22:26
  • Well, this is weird. This code on my computer does not even take 0.2s and it's even faster on release (expcetions out at 4,0). Using `BigInteger` makes absolutely no difference @davidbak – Camilo Terevinto Sep 27 '17 at 22:34

1 Answers1

1

Keep a track of already calculated values for the given parameters and return the cached value instead of calculating it again and again.

shadow32
  • 464
  • 3
  • 9