1

I'm trying to verify the 3x + 1 problem, by running a PHP script online and evaluating all numbers.

I have an infinite loop running, but the server stops my loop when the value reaches about 35,000.

I'm guessing the termination is caused when my HTTP connection resets and the server is no longer serving my request.

I want to have it run as long as it can, eating up the server's resources if it wants to. How do I do it? Cronjobs?

Here's the script, the "End" is never printed.

class Collatz_Verify
{
    public function Collatz_Verify()
    {
        // open output file
        $file = 'verified_nums.txt';
        $outFile = fopen($this->NUMBERS_FILENAME, 'a');
    }

    public function verify()
    {
        $num = 0;
        while(1)
        {
            $num += 1;
            # call collatz!!
            if($this->collatz($num) == 1)
                   fwrite($this->outFile, $num);
        }
        print "ah, crap! End!";
    }

    public function collatz($num)
    {
        if ($num == 1)
            return 1;
        if (($num % 2) == 0)
            return $this->collatz($num/2);
        else
            return $this->collatz((3*$num) + 1);
    }
}


// Fire away!
$ver = new Collatz_Verify();
$ver->verify();

?>
6
  • Does the server give an error message when the script stops? Commented Feb 7, 2013 at 15:22
  • 1
    don't run it under a webserver, then. command line php doesn't have resource limits enabled by default. Commented Feb 7, 2013 at 15:23
  • Have you tried set_time_limit? Commented Feb 7, 2013 at 15:23
  • Run it from the command line. Timeouts are imposed (normally) on requests served by a webserver. Commented Feb 7, 2013 at 15:23
  • 1
    Are you reaching the max execution time? php.net/manual/en/function.set-time-limit.php Commented Feb 7, 2013 at 15:23

5 Answers 5

2

You could run it from command line... you'll eventually stop due to lack of resources though.

Running from command line will prevent you from having to do the ini_set mentioned just before my post. (see: http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time)

Sign up to request clarification or add additional context in comments.

5 Comments

mpaepper's right about the while(1) logic -- but to use php via CLI: php.net/manual/en/features.commandline.php - php.net/manual/en/features.commandline.usage.php
the easiest way is via ssh, or if you have direct access to the machine or vm, open a terminal
But why does a script executed from CLI ignore the max_exeution_limit in the php.ini?
See the documentation: php.net/manual/en/features.commandline.differences.php max_execution_time 0 (unlimited) PHP in a shell environment tends to be used for a much more diverse range of purposes than typical Web-based scripts, and as these can be very long-running, the maximum execution time is set to unlimited.
2

The end never prints, because you are in a while(1) loop and thus the statement afterwards will never be reached. Your script is probably terminated because of your settings in php.ini for the variable max_execution_time

Edit: Try to use the function set_time_limit() https://www.php.net/manual/en/function.set-time-limit.php to be able to increase the time in your script.

1 Comment

oh! interesting. But my web server disabled it. Warning: set_time_limit() has been disabled for security reasons. I'll edit the php.ini
1

php has a max_execution_time limit, If you want it to run for longer, you could try ini_set('max_execution_time' ,100 ); //Replace 100 with how many seconds you want it to be able to run

2 Comments

you also in your php.ini file ( usually /etc/php.ini ) look for max_execution_time and edit it there as well
yeah, it's set to 30 currently.
1

There are two possible reasons for PHP to stop executing your loop.

The first is that the execution time limit is reached. The second is that the maximum nesting level is reached (due to recursion in collatz()).

The maximum nesting level is 100 (I'm not aware of ways to change that). The default execution time limit is 30 seconds. You can increase that, as the other answers show.

To find out what the reason is that your script stops, you should set display errors to on and/or check the error log of the web server. Also you could check the output file and start with the last successful number. So if the last number in the output file is 34998 then change the line $num = 0; into $num = 34998;.

Comments

0

In the PHP.ini file there are limits on time for the script to execute/memory that the script can use.

See http://php.net/manual/en/info.configuration.php for full details

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.