Ackermann function Ackerman

Ackermann function Ackerman

The same algorithm is small after-school job, have not seen before this function (I ignorant)

Examples Ackermann function (Ackermann) of the original non-recursive functions. It takes two natural numbers as an input value, the output of a natural number. Its output value grew very fast, only for the (4,3) output has been so large that can not be accurately calculated.

Recursive formula
Here Insert Picture Description

The code, which is recursive method, this method using ACM then usually time out, because in general this number m = 3 will be considered a great many times, it is generally 0 to 3, m can take

	public static long ackerman(long m, long n) {
        if (m < 0 || n < 0)
            return -1;
        else if (m == 0)
            return n + 1;
        else if (m > 0 && n == 0)
            return ackerman(m - 1, 1);
        else  //m>0 and n>0
            return ackerman(m - 1, ackerman(m, n - 1));
    }

m can only take 0,1,2,3, so that you can put all of the cases listed m the
deduced binding to m = m. 1 when pushed = 0
is derived when m = 2 can be obtained when using the same token. 1 m =
m = . 1 + n-0;
m =. 1 + n-2;
m = 2 2 n-+. 3
m. 3 DP = [. 3] [I] 2 =
DP [. 3] [I-. 1] +3;

	public static long ackerman(long m, long n) {
        if (n == 0)
            return ackerman(m - 1, 1);
        else if (m == 0)
            return n + 2;
        else if (m == 1)
            return n + 2;
        else if (m == 2)
            return 2 * n + 3;
        else if (m == 3)
            return ackerman(m, n - 1) * 2 + 3;
    }

Please help point a praise Thank you! !
Here Insert Picture Description

Published 41 original articles · won praise 94 · views 9564

Guess you like

Origin blog.csdn.net/qq_41718454/article/details/104388645