0
votes

So I have this user defined function:

        function ackermann($n, $m)
        {
            if ($n == 0)
            {
                return 1 + $m;
            }

            if ($m == 0)
            {
                return ackermann($n - 1, 1);
            }

            return ackermann($n - 1, ackermann($n, $m - 1));
        }

        echo ackermann(3, 3);

This should return the value of 61, but it returns this fatal error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in C:\xampp\htdocs\PhpProject1\index.php on line 316

This is only a part of a set of programs that we are tasked to code in PHP since I am taking an introductory subject to PHP. What exactly is the problem?

2
What are you trying to achieve? So much recursion... - Sougata Bose
this function is show result 61 , not error 3v4l.org/MmGcK - Dave
This code works fine at my end may be there something else cause this error can you please share full code - Denis Bhojvani
@Paradigm : Have you placed this code in a seperate file an run it. Seems some error occuring due to some other reason - shadab.tughlaq
Check line 316 on PhpProject1\index.php. - shadab.tughlaq

2 Answers

0
votes

That's because the PHP is unable to complete the request within the memory assigned to it.

Solution is to increase memory_limit in php.ini. It should be 128M by default, change it to 512 or 1024;

memory_limit = 512M;

If it still gets exhausted, try to analyse your code and check if it's not executing in loop.

0
votes

That is because of this:

return ackermann($n - 1, ackermann($n, $m - 1));

You are trying to call ackermann function in ackermann function, which tries to call ackerman function, which calls ackerman function...

It won't ever work then.

What do you want to achieve?